1

当我在我的应用程序中按下此 pushtotalk 按钮时,会在线程内按下键盘按钮 9、*、# 等,按下之间的延迟会暂时禁用该按钮。但是自从我开始使用这个按钮后,有时手机会重启,知道为什么吗?我没有正确处理线程还是什么?

public void pushtotalk2(final View view) {

        final boolean on = ((ToggleButton) view).isChecked();
        ((ToggleButton) view).setEnabled(false);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Instrumentation inst = new Instrumentation();
                    if (on) {

                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_NUMPAD_MULTIPLY);
                        Thread.sleep(500);
                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_9);
                        Thread.sleep(500);

                        runOnUiThread(new Runnable() {
                            public void run() {
                                ((ToggleButton) view).setBackgroundResource(R.drawable.btn_blue_glossy);
                                ((ToggleButton) view).setEnabled(true);
                            }
                        });
                    } else {
                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_POUND);
                        Thread.sleep(500);
                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_9);
                        Thread.sleep(500);
                        runOnUiThread(new Runnable() {
                            public void run() {
                                ((ToggleButton) view).setBackgroundResource(R.drawable.btn_lightblue_glossy);
                                ((ToggleButton) view).setEnabled(true);
                            }
                        });
                    }
                } catch (InterruptedException e) {
                    Log.d(TAG, "Failed to send keycodes: " + e.getMessage());
                }
            }
        }).start();
    }

这里有什么问题吗?

4

0 回答 0