First worship the Great God's post, from the perspective of the source of the analysis of Android handler mechanism. Link:android message processing mechanism (figure + source analysis)--looper,handler,message
Here is not to explain, just do some introduction, want to see the details please poke the above link.
Android's messaging mechanism consists of three blocks of Looper,handler,messagequeue. We know that in the Android neutron thread is not able to perform the component Update operation, since then we can only in the sub-thread to send the message to the main thread, let the main thread to update itself, here will use the handler. A child thread sends a message to the MessageQueue managed by the looper of the main thread by calling the main thread's handler object SendMessage, and the message loop receives the message and invokes the handler function of the main thread to process the message. This enables inter-thread communication.
In activity, there is a default message queue and message loop, and when creating handler (overriding the Handlemessage method), handler itself contains three objects
final MessageQueue Mqueue; The associated MQ
final Looper Mlooper; The associated Looper
final Callback mcallback;
associates its own three objects with the current thread Looper and MessageQueue. Calling SendMessage in a child thread sends the message to the MessageQueue of the handler thread, and the last message loop calls Handler's Handlemessage ()
in Android, The can have its own message queue and message loop written. The child threads created by default are not message queues and message loops, and if you want the child threads to have Message Queuing and message loops, you will need to call Looper.prepare () in the thread to create the message queue and then call Looper.loop () to enter the message loop.
For example:
Public class extends Thread { @Override publicvoid run () { // Initializes the current thread to Looper thread looper.prepare (); // ... Other processing, such as instantiation of handler// start loop processing message Queue looper.loop ();} }
Inside the Looper.prepare, a Looper object is created and placed inside the threadlocal, which can be looper.mylooper to get the Looper object of the current thread through Looper.getmainlooper Gets the Looper object of the current thread's main threads.
Public Static Final Looper Mylooper () { // call Looper.mylooper () on any thread returns the Looper return of that thread (Looper) sthreadlocal.get (); }
Calling Looper.loop () turns on the message loop.
Finally, there are several points to note:
1. Although the message has a public default constructor, you should use Message.obtain () to obtain an empty message object from the message pool to conserve resources.
2. If your message only needs to carry simple int information, prioritize using MESSAGE.ARG1 and message.arg2 to pass information, which is more memory-saving than using bundles
3. Use Message.what to identify information so that you can handle the message in different ways.
Android Message processing mechanism-looper,handler,messagequeue