扎根访问Android中的事件输入节点 [英] Accessing event input nodes in Android withour rooting

查看:83
本文介绍了扎根访问Android中的事件输入节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将不同的事件注入Android设备.经过一番搜索,我发现我可以通过访问Android OS中的事件输入节点来做到这一点,这些节点位于 dev/input/eventX 中.一旦访问了它们,就可以进行读写操作,因此可以注入事件.

I want to be able to inject different events into an Android device. After some search, I found that I can do this by accessing event input nodes in Android OS, which are found in dev/input/eventX. Once these are accessed, read and write operations can take place, and hence I can inject events.

问题在于这些节点只能在有根设备中访问.如果我尝试不生根地使用它们,则该过程将失败,如本文所述

The problem is that these nodes are only accessible in a rooted device. If I try to use them without rooting, the process will fail as mentioned in this article,

http://www.pocketmagic.net/programmatically-injecting-events-on-android-part-2/

我不想植根设备以保留其保修.我在网络上搜索了访问Android OS的可能方法,但我只发现自己正在扎根.

I don't want to root the device to preserve its warranty. I've searched the web for possible ways to accessing Android OS, but I only found rooting.

我认为它将起作用的另一种方法是将应用程序编译为系统应用程序,但是我无法确定这是否将允许它具有对事件输入节点的访问权限(包括读取和写入权限).此方法将提供这些特权吗?

The alternative way which I think it would work is compiling the application as a system application, but I couldn't found whether this will allow it to have access (both read and write privileges) to event input nodes. Will this method provide these privileges?

如果没有,是否还有其他方法可以生根,我可以在不生根设备的情况下为我的应用程序授予系统权限?

If not, is there any alternative way to rooting, where I can give system permissions to my application without rooting the device?

感谢您的帮助.

谢谢.

编辑:要详细说明,我想添加其他触摸事件.例如,单点触摸,滑动等.

EDIT: To elaborate more, I want to inject different touch events. For example, single touch, swipe, etc.

推荐答案

您可以通过执行Android随附的/system/bin/input 实用程序在设备上注入输入事件.您可以在此问题中看到使用它的一些示例(通过adb).输入实用程序似乎不需要任何特殊特权即可执行.

You can inject input events on a device by executing the /system/bin/input utility that ships with Android. You can see some examples of it being used (via adb) in this question. The input utility does not appear to need any special privileges to execute.

要创建系统应用程序,您需要访问在构建设备的Android OS时使用的签名密钥-您不能仅修改普通的应用程序以赋予其系统特权.即使可以,它也不会授予您root访问权限(尽管您可能使它成为了/dev/input/eventX 设备似乎也允许访问的输入用户组的一部分).

To create a system application, you need access to the signing keys used when the Android OS for your device was built - you can't just modify an ordinary App to give it system privileges. Even if you could, it wouldn't give you root access (although you could probably make it part of the input user group which the /dev/input/eventX devices also appear to allow access to).

如果要注入触摸事件,则可以使用 InputManager .

If you want to inject touch events, you can either execute the /system/bin/input utility using the exec() method of the Java Runtime class or just use the injectMotionEvent() method in InputManager.

以下是从Android来源获取的方法,该方法显示了如何注入MotionEvent-您可以查看完整的

Below is a method taken from the Android source showing how to inject a MotionEvent - you can view the full source for more info.

/**
     * Builds a MotionEvent and injects it into the event stream.
     *
     * @param inputSource the InputDevice.SOURCE_* sending the input event
     * @param action the MotionEvent.ACTION_* for the event
     * @param when the value of SystemClock.uptimeMillis() at which the event happened
     * @param x x coordinate of event
     * @param y y coordinate of event
     * @param pressure pressure of event
     */
    private void injectMotionEvent(int inputSource, int action, long when, float x, float y, float pressure) {
        final float DEFAULT_SIZE = 1.0f;
        final int DEFAULT_META_STATE = 0;
        final float DEFAULT_PRECISION_X = 1.0f;
        final float DEFAULT_PRECISION_Y = 1.0f;
        final int DEFAULT_DEVICE_ID = 0;
        final int DEFAULT_EDGE_FLAGS = 0;
        MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, DEFAULT_SIZE,
                DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, DEFAULT_DEVICE_ID,
                DEFAULT_EDGE_FLAGS);
        event.setSource(inputSource);
        Log.i(TAG, "injectMotionEvent: " + event);
        InputManager.getInstance().injectInputEvent(event,
                InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
    }

这些方法仅允许您将事件注入到您自己的应用程序窗口中.

These methods only allow you to inject events into your own app windows.

如果要将事件注入到应用程序以外的其他窗口中,则需要在应用程序清单中声明其他权限(READ_INPUT_STATE和INJECT_EVENTS),然后用Android操作系统签名键对应用程序进行签名.换句话说,将事件注入其他应用程序所需的权限永远不会授予普通应用程序(出于明显的原因).

If you want to inject events into other windows not owned by your app, you need to declare additional permissions (READ_INPUT_STATE and INJECT_EVENTS) in your app manifest and sign your App with the Android OS signing keys. In other words, the permissions needed to inject events into other apps are never granted to ordinary apps (for obvious reasons).

这篇关于扎根访问Android中的事件输入节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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