An analysis of Android analog keyboard mouse event _android

Source: Internet
Author: User
Tags message queue prepare socket

Through the socket + instrumentation implementation of analog keyboard mouse events are mainly composed of the following three parts:
Socket Programming:Implement PC and emulator communication and loop monitor
Service Services:The socket listener is placed in the service to achieve the purpose of running in the background. There are two ways to start a service, Bindservice and StartService, and the difference is that the former will cause the startup services to wither away with the activity of initiating service, and StartService will not. Unless explicitly invoked stopservice, it will always run in the background because the service needs to be started through an activity, so using StartService is more appropriate for the current situation
instrumentation send keyboard mouse events:Instrumentation provides a rich set of function interfaces at the beginning of send to implement an analog keyboard mouse, as described below:
Sendcharactersync (int keycode)//keys for sending the specified keycode
Sendkeydownupsync (int key)//keys for sending the specified keycode
Sendpointersync (Motionevent Event)//For analog touch
Sendstringsync (string text)//used to send a string
Note: The above function must be thrown into the message queue in the form of a message. If you make a call to add directly, you cause the program to crash.
For socket programming and service online There are many successful examples, this article is no longer tired, the following highlights the sending of keyboard and mouse simulation events code:

Send keyboard KeyCode:
Step 1. Declaring class handler variables
private static Handler Handler;
Step 2. Loop process Message
Java code:

Copy Code code as follows:

The]//the following functions in the OnCreate method of the activity
private void Createmessagehandlethread () {
Need start a thread to raise looper, otherwise it would be blocked
Thread t = new Thread () {
public void Run () {
LOG.I (TAG, "creating handler ...");
Looper.prepare (); When the main thread is created, it creates a
A default Looper object, and the creation of a Looper object automatically creates a message Queue. Other than the main thread, will not automatically create looper, when needed, by tuning
Use the Prepare function to implement.
Handler = new Handler () {
public void Handlemessage (msg) {
Process incoming messages here
}
};
Looper.loop ();
LOG.I (TAG, "Looper thread Ends");
}
};
T.start ();
}[/font]

Step 3: Throw the message after receiving the pass information in the socket
Java code:
Copy Code code as follows:

[XXFarEastFont-]handler.post (New Runnable () {
public void Run () {
Instrumentation inst=new Instrumentation ();
Inst.sendkeydownupsync (KeyCode);
}
} ); [/font]

touch specifies the coordinates, as in the following example
Java code:
Copy Code code as follows:

[XXFarEastFont-]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 trajectory
Add Motionevent.action_move to the middle of the above method

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.