如何在android中发送关键事件?

Ser*_*nko 8 android android-event

我正在修改Android 4.0.3.r1的自定义导航栏,并希望发送"Home"和"Back"等关键事件.我的应用程序不是一个系统:

IWindowManager mWindowManager = IWindowManager.Stub.asInterface(
                ServiceManager.getService(Context.WINDOW_SERVICE));
mWindowManager.injectKeyEvent( ev, false );
Run Code Online (Sandbox Code Playgroud)

它不起作用,因为我无法android.permission.INJECT_EVENTS从系统应用程序中获取.我怎样才能做到这一点?

Lot*_*tfi 7

以下是罗马答案的一些精确性

BaseInputConnection  mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);
Run Code Online (Sandbox Code Playgroud)


Rom*_*ets 5

BaseInputConnection  mInputConnection = new BaseInputConnection(targetView, true);
mInputConnection.sendKeyEvent(new KeyEvent(...));
Run Code Online (Sandbox Code Playgroud)


Pro*_*one 0

还有InputConnectionsendKeyEvent功能。InputConnection仅 API 级别 3。