Let's talk about summary. logoff is used to run a message loop in a thread. By default, a thread has no message loop associated with it,
To create one, you must call the logoff. prepare method in this thread, and then call logoff. loop to start the message loop until the loop is stopped. Majority
The interaction with message loop is implemented through the Handler class. A typical example has been given in Handler in the previous article. Here we will repeat it for convenience:
LooperThread =
This example shows how to associate a Thread, logoff, and Handler to work together.
Next, let's take a look at the logoff key fields. The Code is as follows:
ThreadLocal<Looper> sThreadLocal = ThreadLocal<Looper> Looper sMainLooper; Thread mThread;
SThreadLocal indicates the logoff object associated with a thread. Each thread has only one logoff object. smainlogoff is The logoff object associated with the UI thread,
What touch events, key events, and other events (messages) sent by the UI are all handled in this logoff. here, by the way, the main logoff is so important that
The Android system has already been created for each app when it is started. You do not need to worry about the client code. The relevant code is in the android. app. ActivityThread. main () method:
CloseGuard.setEnabled( EventLogger.setReporter("<pre-initialized>"= (sMainThreadHandler == = ("ActivityThread" RuntimeException("Main thread loop unexpectedly exited"
Note that the call to logoff is preparemainlogoff (), and then the loop () method. This is a thread we mentioned at the beginning to have
The message loop must do two things. The UI thread has already been done here, so this explains why the UI thread has a message loop at the beginning.
Next, let's take a look at ctor, as shown below:
Looper(= =
Logoff has a private ctor, which initializes mQueue internally and sets mThread to the current thread.
Several related small methods are as follows:
Thread.currentThread() ==
Return the logoff object associated with the current thread, the MessageQueue object, and the help method to determine whether the logoff thread is the current thread.
Next we will look at a set of prepare-related methods:
prepare( (sThreadLocal.get() != RuntimeException("Only one Looper may be created per thread" (Looper. (sMainLooper != IllegalStateException("The main Looper has already been prepared."= (Looper.
The client code uses the parameter-free prepare method, which creates a message queue that can be exited. The Android system calls preparemainloue,
Call the prepare method with the parameter false internally to create a message queue that cannot be exited. The client code will never call this method (because the system has already
Once called, you will throw IllegalStateException ). The sThreadLocal object in prepare (boolean quitAllowed) is set,
A new logoff is assigned to it, and The logoff object related to this thread is generated. Smainlogoff will also be set in the preparemainlogoff Method
To be called after the client code.
Then the loop method of logoff's major play:
Looper me = (me == RuntimeException("No Looper; Looper.prepare() wasn't called on this thread." MessageQueue queue = ident == queue.next(); (msg == Printer logging = (logging != ">>>>> Dispatching to " + msg.target + " " ++ ": " + (logging != "<<<<< Finished to " + msg.target + " " + newIdent = (ident !="Thread identity changed from 0x" + Long.toHexString(ident) + " to 0x" + Long.toHexString(newIdent) + " while dispatching to " + msg.target.getClass().getName() + " " + msg.callback + " what=" +
Loop () first checks whether the logoff associated with the current thread exists. If no logoff exists, a RuntimeException is thrown, indicating that no logoff is associated with the current thread;
Otherwise, enter the message loop. The subject of this infinite for loop is probably like this: retrieve a msg (blocking operation) from MessageQueue. If msg is null, it indicates
No more messages (MessageQueue is exiting). Exit the for loop. Otherwise, the msg is handed over to its target (Handler) for processing, and then the msg is recycled.
Start the next round of the same processing.
Finally, let's look at a group of quit methods as follows:
The quit method indicates that the message loop is exited as soon as possible. In this mode, no pending messages in the queue will be processed. For other considerations, see the doc of the method;
Sometimes you may want to process the messages in the queue and quit without receiving new messages. You can consider the quitSafely method. But the message exceeds the current time.
Instead of being processed, it is recycled. That is to say, the message with the time (msg. when <= now) has been reached before it is finally processed. For example, the current message
The queue requests to exit. If there are 10 messages in your queue that need to be executed after 1 hour, these messages will be recycled and will not be distributed. For more information, see the doc of the method.
This is where the logoff class is analyzed.