WEB开发网
开发学院手机开发Android 开发 android中模拟按键的另外一种方法 阅读

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 = S

在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。

Tags:android 模拟 按键

编辑录入:coldstar [复制链接] [打 印]
赞助商链接