我怎样才能在Android的发送键事件?事件、Android

2023-09-03 22:49:13 作者:乱了少年的心。

我macking自定义导航栏到Android 4.0.3.r1,想发送类似家和后退按键事件。我的应用程序不是一个系统,因此:

I am macking a custom navigation bar to Android 4.0.3.r1 and want to send key events like "Home" and "Back". My application is not a system therefore:

IWindowManager mWindowManager = IWindowManager.Stub.asInterface(
                ServiceManager.getService(Context.WINDOW_SERVICE));
mWindowManager.injectKeyEvent( ev, false );

这是不行的,因为我不能让 android.permission.INJECT_EVENTS 从没有系统的应用。我怎样才能做到这一点?

It doesn't work, because I can not get android.permission.INJECT_EVENTS from not system application. How can I do this?

推荐答案

这为我的作品:

public static void simulateKey(final int KeyCode) {

    new Thread() {
        @Override
        public void run() {
            try {
                Instrumentation inst = new Instrumentation();
                inst.sendKeyDownUpSync(KeyCode);
            } catch (Exception e) {
                Log.e("Exception when sendKeyDownUpSync", e.toString());
            }
        }

    }.start();
}
 
精彩推荐
图片推荐