Monkey Source analysis Three ways to compare Android injection events

Source: Internet
Author: User
Tags root access

Original: Http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/#.VEoIoIuUcaV

Down to the analysis of the monkey event injected into the source code to understand the next under the Android System event injection method, translated a foreign article as follows.


Method 1:using Internal APIsMethod 1: Use internal APIsThis approach have its risks, like it's always with internal, unpublished APIs.

This approach is at its own risk, as does all other APIs that are not officially published outside the organization.

The idea was to get a instance of WindowManager in order to access the Injectkeyevent/injectpointerevent methods.

The principle is to access the Injectkeyevent/injectpointerevent two event injection methods by obtaining an instance of WindowManager.

IBinder Wmbinder = servicemanager.getservice ("window"); Iwindowmanager M_wndmanager = IWindowManager.Stub.asInterface (Wmbinder);
The ServiceManager and windowsmanager are defined as stubs. We can then bind to these services and call the methods we need.

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:

Key Downm_wndmanager.injectkeyevent (New KeyEvent (Keyevent.action_down, keyevent.keycode_a), true);//Key Upm_ Wndmanager.injectkeyevent (New KeyEvent (KEYEVENT.ACTION_UP, keyevent.keycode_a), true);
To send Touch/mouse events use:
Send Touch/mouse Event:
Pozx goes from 0 to screens WIDTH, Pozy goes from 0 to screen heightm_wndmanager.injectpointerevent (Motionevent.obtain (S Ystemclock.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 works fine, but only inside your application

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

The moment you ' re trying to inject Keys/touch events to all other windows, you'll get a force close because of the followin G Exception:

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

E/androidruntime (4908): java.lang.SecurityException:Injecting to another application requires inject_events Permission
Not much joy, as inject_events is a system permission. A possible solution is discussed here and here.

Bitter force, after all, inject_events is the need for system permissions, some of the possible solutions are discussed here and here.

(Translator Note: Please check my previous translation of the "Monkey Source analysis of the WindowManager injection incident how to jump out of the inter-process security restrictions" There is a more detailed description of the problem)


Method 2:using an Instrumentation object Method 2: Use the Instrumentation objectThis was a clean solution based on the public API, but unfortunately it still requires that inject_events permission.
relative to the hidden interface and methods, this is relatively clean (above is hidden, it is necessary to use the Android not clean not recommended method to get to) the way, butUnfortunately it still has the above jinect_events this only system application (basically Android itself provided, such as monkey) is allowed permission issues.
Instrumentation m_instrumentation = new Instrumentation (); M_instrumentation.sendkeydownupsync (KeyEvent.KEYCODE_B);
For touch Events-can use:
The following is a touch event instance:
Pozx goes from 0 to screens WIDTH, Pozy goes from 0 to screens Heightm_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);

All good inside the test application, and would crash instantly when trying to inject keys to outside apps, not because The approach doesn ' t work, but because Android developers has chosen so. Thanks Guys, you rock! not.

in-app operation, no problem at all. But once you jump out of the app to trigger a key event, it crashes. Not because this method doesn't work, but because Android developers have made restrictions. Thank you, Android developers, you are awesome! A fart.
by looking at Sendpointersync's code, you'll quickly see it uses the same approach as presented in Method 1). So this is the same thing, but packed nicely in a easy-to-use API:
by analyzing sendpointers Ync of the corresponding code, you can see in fact instrumentation used to inject the event mode in fact and method mentioned by Windowmanager.injectpointerevents is the same, so wear is the same underwear, Just robotium out to move around when set on a fashionable horn pants, and the above directly call WindowManager way is like wearing only a pair of underwear out of the street difference.

public void Sendpointersync (Motionevent event) {validatenotappthread (); try {(IWindowManager.Stub.asInterface ( Servicemanager.getservice ("window"))). Injectpointerevent (event, true);} catch (RemoteException e) {}}
Method 3:direct Event injection To/dev/input/eventx Method 3: Inject the event directly into the device/dev/input/eventxLinux exposes a uniform input event interface for each device as/dev/input/eventx where X is an integer. We can use it directly and skip the above Android Platform permission issues.

Linux exposes a unified set of event injection interface/dev/input/eventx (where x represents an integer) to the user in the form of a system device. We can skip directly over the platform (Android this opportunity Linux platform) to limit the problem.
For the-to-work, we'll need root access, so the approach only works on a rooted device.

But this requires work, you need to rooted the equipment.

By default the Eventx files has the permission set for 660 (read and write for Owner and Group only). To inject keys from our application, we need to make it writable. So does this first:

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

You'll need root to run the chmod command.

You will need root privileges to run the chmod command.

Monkey Source analysis Three ways to compare Android injection events

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.