In Android, input commands can be used for event injection, such as input tap 100 200
The input code is in
frameworks/base/cmds/input/src/com/android/commands/input/Input.java
Calling is
298 private void injectMotionEvent(int inputSource, int action, long when, float x, float y, float pressure) { 299 final float DEFAULT_SIZE = 1.0f; 300 final int DEFAULT_META_STATE = 0; 301 final float DEFAULT_PRECISION_X = 1.0f; 302 final float DEFAULT_PRECISION_Y = 1.0f; 303 final int DEFAULT_EDGE_FLAGS = 0; 304 MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, DEFAULT_SIZE, 305 DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, 306 getInputDeviceId(inputSource), DEFAULT_EDGE_FLAGS); 307 event.setSource(inputSource); 308 Log.i(TAG, "injectMotionEvent: " + event); 309 InputManager.getInstance().injectInputEvent(event, 310 InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH); 311 }
InputManager . GetInstance (). InjectInputEvent call to InputDispatcher of
injectInputEvent method
The click and other operations in the monkey test are these methods called,
However, calling this method directly has problems in complex scenarios. When there is already a touch on the screen, sending another such inject event will cause the original touch to be cancelled. In the tap method of input, turn down Add a sleep to the up event, and you will find that the original touch point is cancelled after the up event is injected.
One question.
And using sendevent can avoid such problems, but the data format of sendevent is a bit complicated, this is a solution.
A master wrote to record screen processing through getevent, sendevent similar methods
https://github.com/TUSSON/android-event-recorder