Using socket + instrumentation to simulate keyboard and mouse events consists of the following three parts:
Socket programming: Implements PC-to-emulator communication and loop listening
Service: place the socket listener in the service to run the program in the background. Here, we need to note that there are two ways to start a service: bindservice and startservice. The difference between the two is that the former will make the started service disappear with the activity of the started service, startservice does not. Unless the stopservice is explicitly called, it will always run in the background. Because the service needs to be started through an activity, startservice is more suitable for the current situation.
Instrumentation sends keyboard and mouse events: instrumentation provides a wide range of function interfaces starting with send to simulate keyboard and mouse events, as described below:
Sendcharactersync (INT keycode) // The key used to send the specified keycode
Sendkeydownupsync (INT key) // The key used to send the specified keycode.
Sendpointersync (motionevent event) // used to simulate touch
Sendstringsync (string text) // used to send a string
Note: The above functions must be thrown to the message queue in the form of message. If you directly add the call, the program will crash.
There are many successful examples of socket programming and service on the Internet. This article will not go into detail. The following describes the code for sending a keyboard and mouse Simulated Event:
Send the keyboard keycode:
Step 1. Declare the handler variable
Private Static handler;
Step 2. process messages cyclically
Java code:
[Font =] // call the following functions in the oncreate METHOD OF THE ACTIVITY
Private void createmessagehandlethread (){
// Need start a thread to raise Looper, otherwise it will be blocked
Thread t = new thread (){
Public void run (){
Log. I (TAG, "creating handler ...");
Lorule. Prepare (); // when the main thread is created,
And the creation of the logoff object will automatically create a message queue. Other non-main threads do not automatically create logoff.
Use the prepare function.
Handler = new handler (){
Public void handlemessage (Message MSG ){
// Process incoming messages here
}
};
Logoff. Loop ();
Log. I (TAG, "loginthread ends ");
}
};
T. Start ();
} [/Font]
Step 3. Throw a message after receiving the transfer information in the socket
Java code:
[Font =] handler. Post (New runnable (){
Public void run (){
Instrumentation inst = new instrumentation ();
Inst. sendkeydownupsync (keycode );
}
}); [/Font]
Touch specifies coordinates, as shown in the following example:
Java code:
[Font =] Touch point (240,400)
Instrumentation inst = new instrumentation ();
Inst. sendpointersync (motionevent. Obtain (systemclock. uptimemillis (), systemclock. uptimemillis (), motionevent. action_down, 240,400, 0 ));
Inst. sendpointersync (motionevent. Obtain (systemclock. uptimemillis (), systemclock. uptimemillis (), motionevent. action_up, 240,400, 0); [/font]
Simulated sliding track
Add motionevent. action_move in the middle of the above method