在4.0之前版本时,模拟按键消息可以通过以下方式:
long now = SystemClock.uptimeMillis();
KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 0);
IWindowManager.Stub.asInterface(ServiceManager.getService("window")).injectKeyEvent(down, false);
到了4.1版本及以后,
injectKeyEvent不再被IWindowManager实现,编译错误。改为:
InputManager.getInstance().injectInputEvent(down, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);