我怎样才能在Android的发送键事件? [英] How can I send key events in android?

查看:158
本文介绍了我怎样才能在Android的发送键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我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();
}

这篇关于我怎样才能在Android的发送键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆