Android simulated keyboard and mouse events

Source: Internet
Author: User

Using socket + instrumentation to simulate keyboard and mouse events is mainly composed 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 stopservice is explicitly called, it will always run in the background because the service needs to be started through an activity, so 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:

// 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 ...");
Logoff. Prepare ();
Handler = new handler (){
Public void handlemessage (Message MSG ){
// Process incoming messages here
}
};
Logoff. Loop ();
Log. I (TAG, "loginthread ends ");
}
};
T. Start ();

Step 3. Throw a message after receiving the transfer information in the socket

Java code:

Handler. Post (New runnable (){
Public void run (){
Instrumentation inst = new instrumentation ();
Inst. sendkeydownupsync (keycode );
}
});

Touch specifies coordinates, as shown in the following example:
Java code:

Touch point (1, 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 ));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.