您的位置:首页 > 移动开发 > Android开发

android中模拟按键的另外一种方法

2010-05-29 09:38 465 查看
在com.android.commands.input中发现这一方法

private void sendKeyEvent(String event) {
int eventCode = Integer.parseInt(event);
long now = SystemClock.uptimeMillis();
Log.i("SendKeyEvent", event);
try {
KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, eventCode, 0);
KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP, eventCode, 0);
(IWindowManager.Stub
.asInterface(ServiceManager.getService("window")))
.injectKeyEvent(down, true);
(IWindowManager.Stub
.asInterface(ServiceManager.getService("window")))
.injectKeyEvent(up, true);
} catch (RemoteException e) {
Log.i("Input", "DeadOjbectException");
}
}


injectKeyEvent是WindowManagerService的public 方法,虽然WindowManagerService被隐藏掉,但是我们可以通过反射来调用injectKeyEvent。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: