Event Injection
There are three kinds of event injection methods in Android: In the monkey source of the article, we have introduced a method of event injection, but that way Google is more restrictive, basic to our automation does not help; the second is through the internal API, by the literal meaning, Only Android applications can do this, and this method only takes effect in this application, so it is not particularly ideal for us, and one way is to inject events into the device/dev/input/eventx, this time we introduce the Sendevent method, is actually through/ Dev/input/eventx to achieve the event simulation, learned this can be done after the extrapolate, after all the touch operation simulation can be and so on.
Get events
I'll tell you how I did it.
First I entered the getevent command under the phone's shell so that I could receive all the event events on my phone.
This time we press the power button on our phone to see which event information can be received, press the Power key after:
We got the above four messages.
Let's take the first article for an example to analyze what is obtained?
/dev/input/event0: representative Device
0001 represents a type
0074 represents the code for power key (16 binary)
00000001 represents value General 1 for press, 0 for release.
The parameters required by the lookup sendevent are device, type, code, value.
So we can use the following four commands to complete the operation by the Power key, the median sleep length of more than 2 seconds, the system is considered to be long press:
Sendevent/dev/input/event0 1 116 1 (0074 converted to decimal) sendevent/dev/input/event0 0 0 0sleep 3sendevent/dev/input/event0 1 0sendevent/dev/input/event0 0 0 0
SH script
How do we use automation when we know how to simulate the long press of the Power key? Here I'm going to introduce the shell script
It is well known that Android is based on the Linux kernel, and shell scripts can run on Linux, so we try to test whether SH scripts can be run on Android.
1. Complete the script
Because there is no VI inside Android itself, I'm going to write a script on Windows and then import it, with the script content:
#!/bin/shsendevent/dev/input/event0 1 1sendevent/dev/input/event0 0 0 0echo "Down" sleep 3sendevent/dev/input/event 0 1 0sendevent/dev/input/event0 0 0 0echo "up"
2. Import Mobile phone
This step does not need to say much, save the script name as "boy.sh", and then use ADB to import the phone SD card.
3, plus permissions
In general Linux systems, the imported files may not have permissions, so we need to add permission to run again, so I did the following actions:
After running the prompt I do not have permission, obviously just added permission, we have to look at the file:
Carefully see sure did not add permission to succeed, doubt oneself just was wrong, then tried again, or not.
Finally through continuous attempts to find that the original SD card is not able to add permissions, you must put the files in the/data/data/directory can be:
Finally the authority was added successfully.
4. Operation
After the privilege plus success, can't wait to run:
Or hint there is a problem, and then continue to try to know, because the phone's startup command and Linux are still different, use the following startup method:
After the operation is complete, observe the phone, there is a long press the power button after the effect:
Analog Long Press Power key