Comparison of three injection event methods in Android _android

Source: Internet
Author: User
Tags chmod stub

Method 1: Use internal APIs

This approach has the same risk as any other APIs that are not formally advertised externally. The principle is to access injectkeyevent/injectpointerevent These two event injection methods by obtaining an instance of WindowManager.

Copy Code code as follows:

IBinder Wmbinder = servicemanager.getservice ("window");
Iwindowmanager M_wndmanager = IWindowManager.Stub.asInterface (Wmbinder);

ServiceManager and Windowsmanager are defined as stub stubs classes. We bind these services to our needs and access the methods inside. To send a key do the following: Send an event in the following ways:
Copy Code code as follows:

Key down
M_wndmanager.injectkeyevent (New KeyEvent (Keyevent.action_down, keyevent.keycode_a), true);
Key up
M_wndmanager.injectkeyevent (New KeyEvent (KEYEVENT.ACTION_UP, keyevent.keycode_a), true);
 
Send Touch/mouse Event:
Copy Code code as follows:

Pozx goes from 0 to screen WIDTH, Pozy goes from 0 to screen HEIGHT
M_wndmanager.injectpointerevent (Motionevent.obtain) (Systemclock.uptimemillis (),
Systemclock.uptimemillis (), MOTIONEVENT.ACTION_DOWN,POZX, Pozy, 0), true);
M_wndmanager.injectpointerevent (Motionevent.obtain) (Systemclock.uptimemillis (),
Systemclock.uptimemillis (), MOTIONEVENT.ACTION_UP,POZX, Pozy, 0), true);

This method works well in your application, but only in your application.

Once you want to inject the Keys/touch event into another window, you will get a forced shutdown message:

Method 2: Use the Instrumentation object

Relative to the above hidden interfaces and methods, this is relatively clean (above is hidden, so need to use the Android does not recommend the method to obtain) the way, but unfortunately it still has the above jinect_events this is only the system application (basically Android itself provides, such as monkey) to be allowed permission issues.

Copy Code code as follows:

Instrumentation m_instrumentation = new Instrumentation ();
M_instrumentation.sendkeydownupsync (Keyevent.keycode_b);

Here is a touch event instance:
Copy Code code as follows:

Pozx goes from 0 to screen WIDTH, Pozy goes from 0 to screen HEIGHT
M_instrumentation.sendpointersync (Motionevent.obtain) (Systemclock.uptimemillis (),
Systemclock.uptimemillis (), MOTIONEVENT.ACTION_DOWN,POZX, pozy, 0);
M_instrumentation.sendpointersync (Motionevent.obtain) (Systemclock.uptimemillis (),
Systemclock.uptimemillis (), MOTIONEVENT.ACTION_UP,POZX, pozy, 0);

There is no problem with the operation in the application, but once you jump out of the application to trigger the key event, it crashes. Not because this method doesn't work, but because the Android developers are limiting it. Thank you, Android developers, you are so cool! A fart.

By analyzing Sendpointersync's corresponding code, can see in fact instrumentation use of the injection event mode in fact and the method mentioned through the windowmanager.injectpointerevents is the same, so wear is the same underwear, Just robotium out to walk around when a fashion horn pants, and the above direct call WindowManager way is like wearing only a pair of underwear out of the street difference.

Copy Code code as follows:

public void Sendpointersync (Motionevent event) {
Validatenotappthread ();
try {
(IWindowManager.Stub.asInterface (Servicemanager.getservice ("window"))
. Injectpointerevent (event, true);
catch (RemoteException e) {
}
}

Method 3: Direct injection of events to device/dev/input/eventx

Linux exposes users to a unified set of event injection interfaces/dev/input/eventx (where x represents an integer) in a system device manner. We can simply skip the problem by skipping over the platforms above (Android is the opportunity Linux platform). But if it takes work, you need to rooted the equipment.

The device file Eventx by default is set to 660 (owner and group members have read and write, and owner is root). In order to inject an event into this device, you must make it writable. So please do the following actions first:

Copy Code code as follows:

ADB shell
Su
chmod 666/dev/input/event3

You will need root permission to run the chmod command.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.