Android-服务应用程序如何将KeyEvents调度到另一个前景应用程序 [英] Android - How can a Service App dispatch KeyEvents to another Foreground App

查看:136
本文介绍了Android-服务应用程序如何将KeyEvents调度到另一个前景应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个Android Service应用程序,该应用程序将KeyEvents调度到前台应用程序.

I want to develop an Android Service app that dispatches KeyEvents to the foreground application.

不知何故,就像这样

我似乎找不到一种方法,我只熟悉活动将KeyEvents调度到自身,就像这样:

I cant seem to find a way how, I am only familiar with the activity dispatches KeyEvents to itself, like this:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (event.getKeyCode() == KEYCODE_1) {
            // Do something
        } else if (event.getKeyCode() == KEYCODE_2) {
             // Do something
        }
     } else if (event.getAction() == KeyEvent.ACTION_UP) {
        if (event.getKeyCode() == KEYCODE_1) {
            // Do something
            return false;
        } else if (event.getKeyCode() == KEYCODE_2) {
            // Do something
            return true;
        } 
     }
     return super.dispatchKeyEvent(event);
 }

,但不与另一个App一起使用.

but not with another App.

如果我错了,请更正我,根据到目前为止的研究,它说我需要将我的应用程序安装为系统应用程序,并将关键事件直接发送到android系统,而android系统将是其中一个谁会将其发送到FOREGROUND应用.看起来像这样的地方:

Correct me if I am wrong, according to what I have researched so far, it says that I need to install my application as a System App and send my keyevents directly to the android system, and the android system will be the one who will send it to the FOREGROUND App. Where it would look more like this:

如果这是正确的,谁能帮我怎么做?否则,请给我一个很好的解决方法以实现我的目标.谢谢!

If this is correct, could anyone help me how to do this? Or if otherwise, please give me a good workaround to achieve my goal. Thanks!

PS:它将安装在有根设备上

推荐答案

您绝对可以仅使用root来执行此操作,而无需将应用程序安装为系统应用程序.

You can definitely do this with root only and without installing your application as a system app.

可以通过使用 uiautomator框架来实现此目的. .这是一个功能强大的框架,可用于android,目的是对黑匣子进行测试.

The way this can be accomplished is by using uiautomator framework. This is such a powerful framework available for android with the purpose of black box testing applications.

很明显,您将使用此框架的目的与黑盒测试稍有不同,但这没关系.

Obviously you are going to use this framework for a slightly different purpose than black box testing, but that's ok.

UiDevice具有方法 pressKeyCode(int keyCode)可以像这样用于将输入发送到碰巧出现在前景中的任何应用程序:

UiDevice has the method pressKeyCode(int keyCode) that can be used like this to send input to whatever app happends to be in the forground:

UiDevice dev = UiDevice.getInstance();
dev.pressKeyCode(KeyEvent.KEYCODE_1);

使用uiautomator的设置可能有些复杂,请参阅我的其他答案您需要做什么.

The setup to use uiautomator can be a little complex, see this other answer of mine where I describe in more detail what you need to do.

这篇关于Android-服务应用程序如何将KeyEvents调度到另一个前景应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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