Delivery mechanism handler implementation study notes the news android

message passing in front of the handler also said, speaking today about the handler message passing mechanism is how to achieve.
handle is not working alone, in order handler work, also need to have other components, such as looper, Message, MessageQueue.
In Android a corresponding one of Looper thread, a target corresponding to a MessageQueue Looper, see figure, Looper cycles is who it will continue to attempt to acquire the MessageQueue Message; MessageQueue the message queue is used to store the Message, it follows the FIFO principle; the message message is to be passed.
handler at work, first received Message object, the Message object into the tail MessageQueue's, Looper will continue to head out MessageQueue messages from the team, and then sent to the handler objects, let handler object to process the message, even if MessageQueue the whole message is sent to complete, Looper will not stop, he will continue to wait for the handler object is passed message.
Here Insert Picture Description
Therefore, to use handler, there must be a Looper objects in the current thread.
In the main thread, the system will automatically create a Looper object initialization; if you have to create an object in the child thread Looper job.
In one thread, and the MessageQueue Looper only one, but a plurality Handler, which can share a Looper Handler and MessageQueue.

Published 29 original articles · won praise 17 · views 9459

Guess you like

Origin blog.csdn.net/qq_34423913/article/details/104556218