Using Socket + Instrumentation to simulate keyboard and mouse events consists of the following three parts:
Socket programming:Implement communication between the PC and Emulator and implement loop listening
Service:Place the Socket listener in the Service to run 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 variety of function interfaces starting with send to simulate the keyboard and mouse, 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 handler;
Step 2. process messages cyclically
Java code:
Copy codeThe Code is as follows: [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:
Copy codeThe Code is as follows: [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:
Copy codeThe Code is as follows: [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