Comparison of three event injection methods in Android

Method 1: use internal APIs

This method has its own risks like all other APIs that are not officially published internally. The principle is to access the injectkeyevent / injectpointerevent two event injection methods by obtaining an instance of WindowManager.

Once you want to inject keys / touch events into other windows, you will get a forced shutdown message:

Method 2: use the instrumentation object

Compared with the above hidden interfaces and methods, this method is relatively clean (the above ones are hidden, so it needs to use the methods that Android is not clean and does not recommend to obtain), but unfortunately, it still has the above jinect_ Events is a permission problem that only system applications (basically provided by Android itself, such as monkey) are allowed.

There is no problem operating in the application, but it will crash once you jump out of the application to trigger the key event. Not because this method doesn't work, but because Android developers make restrictions. Thank you, Android developers, you are awesome! Shit.

By analyzing the corresponding code of sendpointersync, we can see that the injection event method used by instrumentation is actually the same as that mentioned in the method through windowmanager.injectpointerevents, so they wear the same pair of underwear, except that robotium puts on a pair of fashionable flared pants when walking out, The above method of directly calling WindowManager is just like the difference of walking out of the street wearing only one pair of underwear.

Method 3: inject events directly into the device / dev / input / EventX

Linux exposes a unified event injection interface / dev / input / EventX (where X represents an integer) to users in the form of system devices. We can skip the limitation of the above platform (Android is the platform of Linux). But if this needs to work, you need rooted equipment.

The device file EventX is set to 660 by default (the owner and members of the same group have read and write permissions, while the owner is root). In order to inject events into this device, you must make it writable. So please do the following first:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>