当我在我的应用程序中按下此pushtotalk按钮时,在一个线程内按下键盘按钮9,*,#etc,在按下之间有一个延迟,暂时禁用该按钮。然而,自从我开始使用此按钮以来,有时手机会重启,任何想法为什么?我没有正确处理线程或什么?
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();
}
这里有什么不对吗?