First, Introduction
In Android development, you use Message Queuing to complete inter-thread communication. The thread that uses Message Queuing is the message loop (msg looper). The message loop constantly checks the message queue for new messages. A message loop consists of a thread and a looper; the Looper object manages the thread's message queue.
The main thread of Android is also a message loop, and also has a looper, the main thread all tasks are done by Looper. A message loop consists of a thread and a looper, and the Looper object manages the thread's message queue, so looper constantly crawls the message from the message queue and finishes the task specified by the message.
PS: Thread By default, there is no message loop (Looper), in Android, only the main thread default is Looper (message loop). In the newly created thread, use Looper to create a looper first.
Ii. message and handler
Message is paired with handler, the message is messages, and handler is the object of the message to complete the task.
A message is an instance of a message that implements some instance variables of the message class at the same time,
-
- What: User-defined int type message code to describe the message.
- OBJ: The user-specified object, which is the message-passing data, that is sent with the message.
- Target: The completion message specifies the target of the task, which is the object used to complete the message-specific task.
Handler is an object that completes a message or message assignment, and handler is not only the target or object of the completion message, but also the interface for creating and publishing messages (message).
(Figure from the Android authoritative guide)
- The Looper has a message queue for messages, so that message must be published or read on Looper.
- A handler is associated with only one looper, and a message is associated with only one target handler (message destination). Instead, Looper owns the entire queue of messages.
Take a look at the diagram below,
(Figure from the Android authoritative guide)
As you can see from the above figure, there are multiple handler associated with a looper, which means that one handler message is placed in the same messages queue as the other handler message.
Third, the use of handler
1. Using Handler.obtainmessage (...) Method gets the message from the public loop of the messages queue.
2. Using the Handler.sendtotarget () method, the method message is sent to the handler associated with the message. The handler will place the message at the end of the Looper queue.
3. Looper in a message queue, gets a specific message and sends it to the message destination (handler associated with it) to be processed. Messages are generally in the handler (message destination) of the Handlermessage (...). The implementation method processes the message to specify the task to complete. Generally, to inherit the handler base class, overwrite the Handlermessage () method.
Iv. Transmission of Handler
An instance of the handler class that can be passed to other sub-threads as a function parameter.
The main thread has a handler and Looper message loop, in which a handler is created on the main thread and automatically associated with the looper of the main thread. The handler that is created by the main thread is passed to another child thread, and the handler that is passed is always kept in contact with the looper of the thread that created it. Therefore, any outgoing handler that are responsible for processing the message will be processed in the message queue of the main thread.
Then, in this way, you can update the main thread's events, data, or UI in the child threads.
V. In a new thread, create a looper (message loop)
Android Looper, Handler and Handlerthread