Android message loop analysis

Source: Internet
Author: User

Our regular use of the system. The work of the program is usually the event-driven and message-driven two ways, in the Android system, Java applications rely on message-driven to work.

The message-driven principle is:
1. There is a message queue. Ability to deliver messages to this queue;
2. There is a message loop. The message is continuously removed from the message queue. Then proceed with the processing.


Encapsulates the message loop via Looper in Android. At the same time, a message queue MessageQueue is encapsulated.
In addition, Android provides us with a package class. To run the delivery of messages, the processing of messages. That is handler.


<!--more-->

When a message loop is implemented in our thread. You need to create looper, such as:
class Looperthread extends Thread {public Handler mhandler;        public void Run () {looper.prepare ();//1. Call Prepare ...    Looper.loop (); 2. Enter the message loop}}  

Look at the code above. The fact is to prepare the looper first and then into the message loop.
1. At the time of prepare. Create a looper, and at the same time create a Message Queuing MessageQueue in the Looper constructor method. Save Looper in TLV at the same time(这个是关于ThreadLocal的,不太懂。以后研究了再说)
2. Call loop to enter the message loop. The fact here is to constantly get messages to the MessageQueue Message and handle them.

And then we'll see how we can use handler to send messages to the queue and process messages

Members of the handler (not all):

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

Member of message (not all):

Handler target;            Runnable callback;

Able to see handler members including Looper, by looking at the source code, we can find that this looper is available in two ways. 1 is passed in the constructor function. 2 is looper using the current thread (assuming that the current thread is not looper. The error will be. We create handler in activity without having to pass handler because the activity itself already has a looper), MessageQueue is the message queue in Looper.

Then we see how to send a message to the message queue, Handler has a very many ways to send the queue (this can be checked), for example we 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 () Get boot up to today's time}//finally all the messages are through this hair, Uptimemillis is the absolute time (from the start of the second counting) public boolean Sendmessageatt IME (Message msg, long Uptimemillis) {Boolean sent = false; MessageQueue queue = Mqueue; if (queue! = null) {Msg.target = this; Sent = Queue.enqueuemessage (msg, uptimemillis); } return sent; }

Look at the code above. You can see the target that handler set itself as a message, and then put the MSG in the queue. and specify the run time.

Message processing

Processes the message, that is, after the queue is fetched from the MessageQueue, the DispatchMessage method of the Msg.target is called to process the Looper. This is handled according to the priority of the message processing:
1. If MSG itself is callback. to deal with it;
2. If the handler has a global callback, it is disposed of;
3. Neither of the above. Then to the handler sub-class implementation of the Handlemessage processing, at this time need to overload handlemessage.

We usually take the third way to deal with it.

Attention!

!!

! We usually use multithreading. When you create a handler, the Looperthread may not have finished creating the Looper, at this point. There is no looper in handler. The operation will error.

We can use Android to provide us with the handlerthread to solve. The class has created the Looper, and through the wait/notifyall to avoid the occurrence of errors, reducing our repeated car-making things.

After we create the object, call Getlooper () to get Looper (Looper waits when it is not created).

Add

This article belongs to the Java layer's message loop mechanism in Android, which also has a message loop in the native layer. There is a separate looper. And after 2.3 The core of MessageQueue to the native layer down, native layer Java layer can be used.

I don't have much research on this!

Ha ha

PS: In-depth understanding of Android: Volume I

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

Android message loop analysis

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.