Message a unit of data that communicates between threads and encapsulates data. Create an existing Message directly from an internally maintained Message pool by calling its static method message.obtain (). 3, common attributes: int what: Int arg1,arg2:Message Object Object obj:Message Object Object 2) Handler target: Each message can record which Handler object is processed. 3) Runnable callback: 4) Message Next: A Message object is maintained internally within each Message to form a Message queue. The Handler is the Handler that processes Message and is responsible for the sending and removing of messages

2. Send messages

There are four methods, which I won’t explain


sendEmptyMessage(int what)

sendEmptyMessageDelayed(int what,long millitime)

sendMessage(Message mess)

sendMessageDelayed(Message mess,long millitime)

Ps: The delay message is sent immediately, and the delay is processed. 3

RemoveMessages (int what) removeMessages(int what) removeMessages(int what) RemoveCallbacksAndMessages (null) MessageQueue: 1, used to store the Message queue of messages sent through the Handler 2, it is a priority queue according to the Message when ordering. MessageQueue (when: indicates the time when the Message was processed) Looper (when: indicates the time when the Message was processed) See how they work together



1, obtain a Message object from the Message pool using message.obtain (), assign values to related properties (what, etc.)

2. The Handler object sends the message (delayed message is sent immediately, delayed processing).

3. The sent messages are sorted in MessageQueue (by the when attribute)

4. Looper retrieves messages from MessageQueue and may block (there are no messages in the MessageQueue or the last message has not been processed yet). When blocked, Looper falls asleep (not wasting memory) and wakes himself up when the time is up

Looper calls the Handler object’s dispatchMessage() method to distribute the message. There are three ways to handle messages: Callback of Message (a Runnable object), callback of Handler, and handleMessage of Handler. The first two methods have higher precedence but are rarely used, usually null.

6. The Handler object calls the handleMessage method to manipulate the UI (in the main thread).

7. After the message is processed, it is removed from the message queue, and Looper is responsible for cleaning up the message (all attributes are returned to their original values) and putting it into the message pool for reuse

Sending and processing messages are the same object