Android message loop analysis

Source: Internet
Author: User

In our common systems, the program usually works in two ways: event-driven and message-driven. In Android systems, Java applications work by message-driven.

The message-driven principle is:
1. There is a message queue that can deliver messages to this queue;
2. There is a message loop that constantly extracts messages from the message queue and then processes them.
In Android, message loops are encapsulated by logoff, and a message queue MessageQueue is encapsulated.
In addition, Android provides an encapsulation class for message delivery and processing, that is, Handler.

When we implement a message loop in our thread, we need to create logoff, such:
Class LooperThread extends Thread {public Handler mHandler; public void run () {loid. prepare (); // 1. call prepare ...... logoff. loop (); // 2. enter message loop }}

Looking at the above Code, we are actually preparing logoff first and then entering the message loop.
1. During prepare, create a logoff and create a message queue MessageQueue In The logoff constructor. At the same time, save the logoff to TLV.(I don't know much about ThreadLocal. I will discuss it later)
2. Call the loop to enter the message loop. In this case, the message is continuously retrieved from MessageQueue.Message.

Then let's look at how we use Handler to send messages to the queue and process messages.

Handler members (not all ):

final MessageQueue mQueue;    final Looper mLooper;    final Callback mCallback;

Message members (not all ):

Handler target;            Runnable callback;

We can see that the Handler member contains logoff. by viewing the source code, we can find that this logoff is obtained in two ways. 1 is passed in by the constructor, 2 is to use the Logoff of the current thread (if the current thread has no logoff, an error is returned. The Handler we create in the Activity does not need to pass Handler because the Activity already has a logoff), MessageQueue is also the message queue in the logoff.

Then let's look at how to send messages to the Message Queue. Handler has many methods to send messages to the queue (this can be checked by itself). For example, let's look at sendMessageDelayed (Message msg, long delayMillis)

Public final boolean sendMessageDelayed (Message msg, long delayMillis) {if (delayMillis <0) {delayMillis = 0;} return sendMessageAtTime (msg, SystemClock. uptimeMillis () + delayMillis); // SystemClock. uptimeMillis () gets the time from boot to the present} // all the final messages are sent through this Message. uptimeMillis is the absolute time (counted from the second when boot) public boolean sendMessageAtTime (Message msg, long uptimeMillis) {boolean sent = false; MessageQueue queue = mQueue; if (queue ! = Null) {msg.tar get = this; sent = queue. enqueueMessage (msg, uptimeMillis);} return sent ;}

Looking at the code above, we can see that Handler sets itself as the target of the Message, then puts the msg in the queue, and specifies the execution time.

Message Processing

When looperates the message from messagequeue, the dispatchMessage method of msg.tar get is used for processing. The priority of message processing is as follows:
1. If msg itself has callback, it will be handed over for processing;
2. If Handler has a global callback, it will handle it;
3. If neither of the preceding two types is available, the handleMessage is handed over to the Handler subclass for handleMessage processing. In this case, handleMessage must be overloaded.

We usually use the third method for processing.

Note !!!! We generally use multiple threads. When Handler is created, LooperThread may not be created yet. At this time, there is no logoff in Handler, and an error will be reported.

We can use HandlerThread provided by Android to solve this problem. This class has already created logoff and uses wait/policyall to avoid errors and reduce repeated car creation tasks. After this object is created, call getlogoff () to obtain the logoff (waiting when the logoff is not created ).

Supplement

This article belongs to the java-layer message Loop Mechanism in Android. It has a message loop in the Native layer and has a separate logoff. In addition, after 2.3, the core of MessageQueue is moved down to the Native layer, which can be used by the native layer java layer. I have not studied much! Haha

PS: For more information about Android: Volume I, see this article.

Original address: http://blog.isming.me/blog/2014/04/02/android-message-loop-analyze/, reproduced please indicate the source.

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.