android中模拟按键的另外一种方法
2010-05-31 14:17:00 来源:WEB开发网在com.android.commands.input中发现这一方法
view plaincopy to clipboardprint?
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");
}
}
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。
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
赞助商链接