I will talk about Handler, loader and MessageQueue in my understanding of the message mechanism in Android, and loopermessagequeue.
Handler is used to send messages and process messages. The messages sent by handler are actually sent to their own objects for processing. Therefore, whoever sends the messages is who processes the messages, but this is absolutely meaningful, in this way, we can use Handler to transfer the processing of a Message from one thread to another. After the Message has been transferred, although the object to be processed is the same, but the thread that processes it changes to the thread that creates the Handler object, rather than the thread that generates the Message object (of course, the two threads may be one, but using handler in this way is the second purpose.) The second purpose of using handler is to process a Message at a specific time in the future, that is, to process delayed messages, for example, handler's postDelayed (), sendMessageDelayed (), or sendMessageAtTime () method.
Logoff associates a MessageQueue and polls the MessageQueue to call its loop () method. To create a logoff object, you must call its prepare () static method to create it. It cannot be new. Which thread creates a lo and which thread is responsible for calling the Looper loop () method for Message polling.
MessageQueue is used to maintain a message queue and provide it to logoff for polling. logoff contains references to MessageQueue. Once a logoff object is created, a corresponding MessageQueue object is created, this MessageQueue object is also a local message queue, because the native method is called when it is created.
Relationship between Handler and logoff and MessageQueue: In a Handler object, there are both logoff objects and MessageQueue objects. MessageQueue is obtained through an mQueue attribute of its logoff object, the logoff object is created by creating the Handler thread (the Static Method prepare () must be called to create the logoff object and cannot be created directly ), so we can say that MessageQueue in Handler belongs to the thread that created this Handler, and it is actually used to round the MessageQueue through the loop () method of The logoff object created by this thread.
The logoff object is created through the current thread that creates the Handler, and it must be ensured that this thread is going to call the logoff object's loop () method for Message polling (in loop () some native methods are called internally to complete the local message polling). If you want to exit the polling, you can call the quit () method of The logoff object. The UI thread (main thread) has helped us do all this. Therefore, if we create Handler in the main thread, the main thread will automatically help us round robin our MessageQueue, therefore, we can call post () and sendMessage () of the Handler object in the Child thread to process our Message in the main thread (UI thread) and in the main thread (UI thread).
In fact, the four methods post (), postDelayed (), sendMessage (), and sendMessageDelayed () are essentially called sendMessageAtTime (), but post only wraps the runnable object, it is encapsulated into a Message object, and the callback of this Message object is runnable. Finally, when we select the Message processing method, check whether the Message object has a callback object. If yes, call this callback object and check whether the entire handler object has a callback object. If yes, call the handleMessage () method of this callback object to process the Message object, if not, call the handleMessage method of the handler object to process the Message. Therefore, Message processing has a priority.
Handler, logoff, and MessageQueue
Handler includes logoff and MessageQueue of logoff;
Logoff contains MessageQueue,
Therefore, logoff belongs to Handler, which is created and used by the thread that creates Handler, and MessageQueue belongs to logoff. The logoff loop () method is called by the thread that creates Handler for polling.
When we create a Handler object in the main thread (UI thread), the Handler object is associated with a logoff object, which is not created by us, it was created by the ActivityThread class in the Activity in the main function (the main function is the portal function of the entire Android Application), as follows:
public static void main(String[] args) { SamplingProfilerIntegration.start(); // CloseGuard defaults to true and can be quite spammy. We // disable it here, but selectively enable it later (via // StrictMode) on debug builds, but using DropBox, not logs. CloseGuard.setEnabled(false); Environment.initForCurrentUser(); // Set the reporter for event logging in libcore EventLogger.setReporter(new EventLoggingReporter()); Security.addProvider(new AndroidKeyStoreProvider()); // Make sure TrustedCertificateStore looks in the right place for CA certificates final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId()); TrustedCertificateStore.setDefaultUserDirectory(configDir); Process.setArgV0("<pre-initialized>"); Looper.prepareMainLooper(); ActivityThread thread = new ActivityThread(); thread.attach(false); if (sMainThreadHandler == null) { sMainThreadHandler = thread.getHandler(); } if (false) { Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, "ActivityThread")); } Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }
From the code above, we can see that the logoff object is created through logoff. preparemainlogoff (), and the polling of the message starting by calling loop () is at the end: logoff. loop (). Through the source code, we can see that loop () is an endless loop of for (;) {}, which is the main thread loop of the entire application. When we develop the window application, we know that this application is a large endless loop called main thread loop, and the main thread loop of the Android Application is here. Now we know how our messages have been polling and processed! Find the main loop and everything is done!
Conclusion: Handler is responsible for sending and processing messages. logoff is responsible for receiving messages and polling messages, and forwarding messages to Handler itself. Handler processes messages in different threads; messageQueue is a Message container. logoff contains references to the MessageQueue object, which is used to store the data structure of the Message object in the form of linked list ), logoff can complete message round-robin (through the loop () method ).