模拟物理按键
2018-06-13 本文已影响0人
大白520
方法1:
//物理按键模拟
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
注意:不能再UI进程使用,否则报错"java.lang.RuntimeException: This method can not be called from the main application thread"
方法2:
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec("input keyevent " + KeyEvent.KEYCODE_BACK);
} catch (IOException e) {
e.printStackTrace();
}