Method 1: Use internal APIs
This approach is at its own risk, as does all other APIs that are not officially published outside the organization. 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);
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);
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 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:
E/androidruntime (4908): java.lang.SecurityException:Injecting to another application requires inject_events Permission
Bitter force, after all, inject_events is the need for system permissions, some of the possible solutions are discussed here and here . ( Heaven Zhuhai Branch rudder Note: Please check my previous translation of "Monkey Source analysis of the WindowManager injection incident how to jump out of the inter-process security restrictions " There is more detailed description of the problem )
Method 2: Use the Instrumentation object
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 obtain), but unfortunately it still has the above jinect_events this only system application (basically Android itself provided, such as monkey) are allowed permission issues only.
Instrumentation m_instrumentation = new Instrumentation (); M_instrumentation.sendkeydownupsync (KeyEvent.KEYCODE_B);
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);
In-app operation is completely fine, 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 analyzing the corresponding code of the Sendpointersync, can see in fact instrumentation used to inject event mode actually and method one 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: Inject the event directly into the device/dev/input/eventx
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. But this requires work, you need to rooted the equipment.
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:
ADB shellsuchmod 666/dev/input/event3
You will need root privileges to run the chmod command.
|
Work/Translator |
Knowledge sharing public Number |
CSDN |
|
Heaven Zhuhai Branch Rudder |
Techgogogo |
Http://blog.csdn.net/zhubaitian |
Three ways to compare Android injection events