Relationship between message, Handler, message Queue, Looper in a single-threaded model
1. Message
A message is an information that can be understood as a communication between threads. Processing a data background thread requires updating the UI, and you can send a message containing some data to the UI thread.
2, Handler
Handler is the processor, is the main processor of the message, responsible for sending the message, message content execution processing. A background thread is a SendMessage (Message) by passing in a handler object reference. With handler, you need to implement the Handlemessage (Message) method of the class.
3. Message Queue
Message queue, which is used to store messages published through Handler, is executed according to the FIFO principle. Each message queue will have a corresponding handler. Handler sends messages to message queue in two ways: SendMessage or post. Both messages are inserted at the end of the message queue and are executed according to the FIFO principle. However, the messages sent by these two methods are slightly different: sending a Message object via SendMessage is handled by the handler Handlemessage () method, and the Post method sends a Runnable object. It will execute itself.
4, Looper
Looper is the steward of each line thread message queue. Android does not have a global message queue, and Android automatically creates a message queue for the main thread (the UI thread), but the message queue is not established on the thread line. So calling Looper.getmainlooper () gets the looper of the main thread is not NULL, but calling Looper.mylooper () gets the looper of the current thread to be null.
Borrow a flowchart from someone else.
The relationship between message, Handler, message Queue, Looper