Messagequeue, logoff, handler and message loop in Android

Source: Internet
Author: User

There are various events in the android activity, which are eventually converted to messages for processing. The message system in Android involves:
* Message sending
* Message Queue
* Message loop
* Message distribution
* Message reading
Important messages include messagequeue, logoff, and handler, which correspond to message queues, message loops, and Message Processing respectively.

 

Handler class:
Handler is mainly used to send messages and process messages. Each handler instance corresponds to a thread and a message queue of the thread.
When you create a handler object, the handler object belongs to the thread that created it and is bound to the Message Queue of the thread. For example, you can create a handler object in the main thread, the handler only belongs to the main thread and is bound to the Message Queue of the main thread. (Of course, each thread has its own message queue. In Android, message queues are targeted at threads ). In this way, the handler can send messages to the message queue and process the messages in the queue.
When you perform time-consuming operations, such as remote network access and downloading, a new thread is usually started. Instead of putting it in the UI thread, this can prevent ANR exceptions caused by Android's failure in 5 seconds. How can I update the returned results in the Child thread to the UI thread? In this case, handler can be used to process the results. You can define handler in the main thread, and then send the messages in the subthread to the Message Queue corresponding to the main thread through the main thread handler. In the main thread, you can use handler. handlermessage to process the message and update it to the UI.
As we said earlier, handler is mainly used to send and process messages. Then, where is the message loop and queue managed. The answer is logoff and messagequeue.

Logoff class:
Logoff is mainly used to enable the message loop of a thread. By default, the system creates a message loop for the main thread at startup. No other newly created threads,
If needed, you can call lorule. Prepare () in this thread to enable the lorule object, and then call lorule. Loop () to enter the message loop.
In this way, the thread has a message loop mechanism, such:

class LooperThread extends Thread {        public Handler mHandler;                public void run() {            Looper.prepare();                        mHandler = new Handler() {                public void handleMessage(Message msg) {                    // process incoming messages here                }            };                        Looper.loop();       }    }

In fact, the message queue is created in logoff. Check the logoff loop () method. This method is used for message loop.

Public static final void loop () {Looper me = mylooper (); // obtain the looper object messagequeue queue = me of the current thread. mqueue; // get the message queue of the current thread while (true) {// The condition is true, which is executed cyclically. Message loop message MSG = queue. Next (); // might block // If (! Me. mrun) {// break; //} If (MSG! = NULL) {If (msg.tar get = NULL) {// No target is a magic identifier for the quit message. Return;} If (Me. mlogging! = NULL) me. mlogging. println (">>>>> dispatching to" + msg.tar get + "" + MSG. callback + ":" + MSG. whclearforrecycleat); msg.tar get. dispatchmessage (MSG); // If (Me. mlogging! = NULL) me. mlogging. println ("<finished to" + msg.tar get + "" + MSG. callback); MSG. recycle (); // The message has been distributed and recycled }}}

Let's look at the handler constructor. In the handler constructor, we can get the logoff object and message queue of the current thread. The message queue is also obtained from logoff, as we said just now.

Public handler () {If (find_potential_leaks) {final class <? Extends handler> Klass = getclass (); If (Klass. isanonymousclass () | Klass. ismemberclass () | Klass. islocalclass () & (Klass. getmodifiers () & modifier. static) = 0) {log. W (TAG, "The following handler class shocould be static or leaks might occur:" + Klass. getcanonicalname () ;}// obtain the logoff object and message queue of the current thread in the handler constructor. Message Queue is also obtained from logoff mlogoff = logoff. mylooper (); If (mloexception = NULL) {Throw new runtimeexception ("can't create handler inside thread that has not called loled. prepare () ");} mqueue = mloue. mqueue; mcallback = NULL ;}

There are also some important methods in handler:

 

Handlemessage (Message) // process the message

Dispatchmessage (Message) // resend the message

Sendmessage (Message) // send a message

 

Here we can roughly understand the message process in Android as follows:
Handler obtains the logoff and messagequeue of the current thread and sends a message to messagequeue. Logoff loops messages and sends messages through msg.tar get. dispatchmessage (target should be handler ). Then handler. handlermessage processes the message.

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.