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 analyze the monkey event injected into the source code before understanding the way under the Android system event injection, 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 Metho DS. 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 way:

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, however only inside your application is a great way to work in your application, but it's only available in your application. the moment you ' re trying to Ject Keys/touch events to anything else window, you'll get a force close because of the following exception: Once you want to inject K into other windows Eys/touch event, 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 objectMethod 2: Use the Instrumentation object

This 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, but Unfortunately 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 Androi D developers has chosen so. Thanks Guys, you rock! not. in-app operation is completely fine, but once you jump out of the app to trigger a keystroke 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: through analysis Sendpointersy NC's corresponding code, you can see in fact instrumentation used to inject event mode actually 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/eventxMethod 3: Inject the event directly into the device/dev/input/eventx

linux 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 interfaces to the user in the form of system devices/dev/input/eventx ( where x represents an integer). We can skip directly over the platform (Android this opportunity Linux platform) to limit the problem. for the work, we'll need root access, so the approach only works on a rooted device. But this requires work, you need to rooted the device. 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: 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. So let's do the following:

ADB shellsuchmod 666/dev/input/event3

You'll need root to run the chmod command. you will need root privileges to run the chmod command.

Author Independent Blog Service number and Scan code CSDN
Heaven Zhuhai Branch Rudder Http://techgogogo.com Service Number : Techgogogo Scan Code : Http://blog.csdn.net/zhubaitian

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.