In our common system, programs work usually in both event-driven and message-driven ways, and in Android, Java applications work by message-driven.
The message-driven principle is:
1. There is a message queue where messages can be delivered to this queue;
2. There is a message loop that continuously extracts messages from the message queue and then processes them.
The message loop is encapsulated in Android via Looper, while a Message Queuing MessageQueue is encapsulated in it.
In addition, Android provides us with a wrapper class to perform message delivery, message processing, i.e. handler.
<!--more-->
When implementing a message loop in our thread, we need to create a looper, such as:
class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); //1.调用prepare ...... Looper.loop(); //2.进入消息循环 }}
Looking at the code above, the first thing is to prepare the looper and then enter the message loop.
1. In prepare, create a looper while creating a message queue MessageQueue in the Looper constructor, and save Looper in the TLV(这个是关于ThreadLocal的,不太懂,以后研究了再说)
2. Call loop to enter the message loop, where the fact is to continue to MessageQueue messages Message
, processing.
And then see how we 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;
You can see that the members of handler contain looper, by looking at the source code, we can see that this looper is obtained in two ways, 1 is passed in the constructor, 2 is using the current thread's looper (if the current thread has no looper, it will be an error.) We do not need to pass handler to create handler in activity 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 there are many ways to send the queue (this can be checked), such as we see 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 power on to the present time}//finally all 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; }
Looking at the code above, you can see that handler set itself as the target of the message, then put the MSG in the queue and specify the execution time.
Message processing
Processing the message, that is, after looper the queue from the MessageQueue, calls the Msg.target DispatchMessage method for processing, which is handled according to the priority of the message processing:
1. If MSG itself is callback, then it is processed;
2. If the handler has a global callback, it is disposed of;
3. None of the above, then to the handler sub-class implementation of handlemessage processing, at this time need to overload handlemessage.
We usually use a third way to deal with it.
Attention!!!! We generally use multi-threading, when creating handler, Looperthread may not complete the creation of Looper, at this time, there is no looper in handler, the operation will be error.
We can use Android to provide us with the handlerthread to solve, this class has created the Looper, and through the wait/notifyall to avoid the occurrence of errors, reduce our repeated car-building 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 of Android in the message loop mechanism, which in the native layer also has a message loop, 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: This article refers to "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.