2) logoff. loop, entering the message loop
/*** Run the message queue in this thread. be sure to call * {@ link # quit ()} to end the loop. */public static void loop () {// here, "me" is the Looper me = myLooper () object stored in the local variable of the thread (); // retrieve the message queue in the logoff, that is, the MessageQueue queue created in prepare () = me. mQueue; // Make sure the identity of this thread is that of the local process, // and keep track of what that identity token actually is. binder. ClearCallingIdentity (); final long ident = Binder. clearCallingIdentity (); while (true) {Message msg = queue. next (); // might block if (msg! = Null) {if (msg.tar get = null) {// No target is a magic identifier for the quit message. return;} long wallStart = 0; long threadStart = 0 ;...... // The actual message is distributed here. // The figure in the first section shows that Message.tar get is Handler msg.tar get. dispatchMessage (msg );...... // Make sure that during the course of dispatching the // identity of the thread wasn' t been upted. final long newIdent = Binder. clearCallingIdentity (); If (ident! = NewIdent) {Log. wtf (TAG, "Thread identity changed from 0x" + Long. toHexString (ident) + "to 0x" + Long. toHexString (newIdent) + "while dispatching to" + msg.tar get. getClass (). getName () + "" + msg. callback + "what =" + msg. what);} msg. recycle () ;}}/ *** Return the locle object associated with the current thread. returns * null if the calling thread is not associated with a logoff. */public static logoff mylogoff () {return sThreadLocal. get ();}
In fact, Handler, MessageQueue, and Message have been introduced from the two key methods of logoff prepare and loop, and the relationship between them can be simply described in the diagram:
-Logoff creates MQ;
-MQ uses a single-chain table structure to store all messages;
-Each Message is associated with a Handler that processes the Message;
-The Handler is used to process the message;
Of course, there are still a lot of details in it. Let's take a closer look at Handler from the key call msg.tar get. dispatchMessage (msg) in the logoff. loop () method.
Handler
I learned more about Handler based on my own problems:
-The Handler also has an mQueue. Is there any difference between the mQueue and the queue created in logoff?
While searching for this answer, it is exactly the three Handler constructor methods:
/** * Default constructor associates this handler with the queue for the * current thread. * * If there isn't one, this handler won't be able to receive messages. */public Handler() { ... ... mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = null;}/** * Constructor associates this handler with the queue for the * current thread and takes a callback interface in which you can handle * messages. */public Handler(Callback callback) { ... ... mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = callback;}/** * Use the provided queue instead of the default one. */public Handler(Looper looper) { mLooper = looper; mQueue = looper.mQueue; mCallback = null;}
The code is very clear. The mQueue here is actually the logoff MQ of the thread where the Handler object is located. The two are the same MQ. In the second constructor, A mCallback member is added. The callback is based on the message name. We will see it in the analysis of dispatch;
-Where does the queue. next () message in logoff. loop () come from, that is, how does the message queue?
The simplest way is to get the MessageQueue object through logoff. getQueue (), and then queue the message through the enqueueMessage () method of MessageQueue. Unfortunately, no. lorule. getQueue () is @ Hide, which cannot be accessed by upper-layer applications. You can use some helper methods of Handler to access:
// Add the Message to the MQ tail public final boolean sendMessage (Message msg) // Add the Message to the MQ header for storing the public final boolean sendMessageAtFrontOfQueue (Message msg) Message with a higher priority) // remove the public final void removeMessages (int what) Message whose Message code is what. // create a public final Message obtainMessage (int what) Message whose Message code is what) // check whether message public final boolean hasMessages (int what) with the message code in MQ is available)
The preceding section only lists the Message Access methods provided by some Handler. For more information, see:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1054453515-0.png "title =" 002.png"/>
The following is the source code process of sendMessage:
/*** Enqueue a message into the message queue after all pending messages * before the absolute time (in milliseconds) <var> uptimeMillis </var>. * <B> The time-base is {@ link android. OS. systemClock # uptimeMillis }. </B> * You will receive it in {@ link # handleMessage}, in the thread attached * to this handler. ** @ param uptimeMillis The absolute time at which the message shocould be * delivered, using * {@ Link android. OS. systemClock # uptimeMillis} time-base. ** @ return Returns true if the message was successfully placed in to the * message queue. returns false on failure, usually because the * logoff processing the message queue is exiting. note that a * result of true does not mean the message will be processed -- if * the logoff is quit before the delivery time of the message * occurs then t He message will be dropped. */public boolean sendMessageAtTime (Message msg, long uptimeMillis) {boolean sent = false; MessageQueue queue = mQueue; if (queue! = Null) {// set the target of the message to handler msg.tar get = this; sent = queue. enqueueMessage (msg, uptimeMillis );}...... return sent ;}
The uptimeMillis parameter indicates the absolute time when a Message is sent. Generally, the application calls the sendMessage (Message msg) method, which calls sendMessage (Message msg, long delayMillis ), in this method, the uptimeMillis parameter is set and the above method implementation is called.
The main action here is to set the target of the Message as the Handler of the call and queue the Message.
-Handler's message dispatching mechanism;
Public void dispatchMessage (Message msg) {// if the Message itself sets a callback, the callback if (msg. callback! = Null) {handleCallback (msg);} else {// if callback is set for Handler, the callback if (mCallback! = Null) {if (mCallback. handleMessage (msg) {return ;}}// if none of them are set, hand it to handleMessage (msg );}}
The message distribution mechanism and process are as follows:
First, determine whether a callback is set for the message to be sent. If yes, submit it to the callback and end the assignment;
If no callback is set for the Message, the Handler determines whether the callback is set. If yes, the callback is sent to the Handler and the assignment ends;
If Handler is not set, call Handler. handleMessage (Message), and The handleMessage is the place where the application needs to process messages.
This article is from the "quiet Madman" blog, please be sure to keep this source http://quietmadman.blog.51cto.com/3269500/1282293