前段时间做了一个PSTN通话的应用程序,PAD连接座机实现通话功能。座机上有上翻、下翻通讯录的功能。通讯录用的是Google默认的。在不改动通讯录的情况下只有使用模拟按键来发送消息。代码很简单,共享下!!
- <span style="font-size:18px;">public class MenuDemoActivity extends Activity {
- protected static final String TAG = "MenuDemoActivity";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final Button button = (Button) findViewById(R.id.button1);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- sendKeyCode(KeyEvent.KEYCODE_DPAD_DOWN);
- }
- });
- final Button button2 = (Button) findViewById(R.id.button2);
- button2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- sendKeyCode(KeyEvent.KEYCODE_DPAD_DOWN);
- }
- });
- }
- /**
- * 传入需要的键值即可
- * @param keyCode
- */
- private void sendKeyCode(final int keyCode){
- new Thread () {
- public void run() {
- try {
- Instrumentation inst = new Instrumentation();
- inst.sendKeyDownUpSync(keyCode);
- } catch (Exception e) {
- Log.e("Exception when sendPointerSync", e.toString());
- }
- }
- }.start();
- }
- }
- </span>
加入权限:
- <span style="font-size:18px;"><uses-permission android:name="android.permission.INJECT_EVENTS" /></span>