In the Android system, some friends may need this function.
When you press a button in the activity, the response is like pressing the Home Key of the hardware. Such a function can be implemented. Why?
Based on the hardware, the driver will generate messages and put them in the system's message queue. I suddenly thought of the message queue of MFC. What is a message queue? Have you ever seen a message queue? The message queue is only a data structure. Two-way connection tables can be message queues. I ran the question.
Since the driver can put messages in the message queue, can the AP generate a message and put it in the message queue? It works in principle.
How to generate a message? How can I transmit messages to the system message queue? If you solve these two problems, you can do it. No matter whether your message is generated by hardware or by software simulation, if there is a message, I will read it and then do the processing.
Well, let's start solving these two problems.
First, we have to understand that I want to simulate a key message. It is a keyevent event.
So how to put it in the message queue? Well, I have read the framework for you.
Let's look at the example.
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. deadobjectexception;
Import Android. OS. servicemanager;
Import Android. View. iwindowmanager;
Import Android. View. keyevent;
Import Android. View. Menu;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Public class simualtekeyinput extends activity {
Final iwindowmanager windowmanager = iwindowmanager. Stub
. Asinterface (servicemanager. getservice ("window "));
Public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Setcontentview (R. layout. Main );
This. findviewbyid (R. Id. pai_simulate_key). setonclicklistener (
New onclicklistener (){
Public void onqisda changerclick (view arg0 ){
New thread (New runnable (){
Public void run (){
Simulatekeystroke (keyevent. keycode_home );
}
}). Start ();
}
});
}
Private void simulatekeystroke (INT keycode ){
Injectkey chunjie event (New keyevent (keyevent. action_down, keycode ));
Injectkeyevent (new key event (keyevent. action_up, keycode ));
}
Private void injectkeyevent (keyevent kevent ){
Try {
Windowmanager. injectchangerchangerkeyevent (kevent, true );
} Catch (exception e ){
E. printstacktrace ();
}
}
}
Well, I'm so glad that I finally came out. But there's a problem. What's the problem?
This Code cannot be run on Android SDK 1.6, and cannot be compiled. Import Android. View. iwindowmanager; the key is that this statement is not supported. This is not supported. You need to have a framework.
If you are interested, take a closer look. How to put a message in a message queue is to connect to windowmanagerservice through the binder and pass the message through iwindowmangaer. aidl.
Well, it's good. I got an education. I will post it soon. You can say, it's really good. Thank you for letting me get an education. Thanks to Suzhou and Jia Shida Yu chunjie.