Android face questions please explain the relationship between message, Handler, MessageQueue, Looper in the single-threaded model

Source: Internet
Author: User

Simply put, handler gets the Looper object in the current thread, Looper is used to hold the message taken out of the MessageQueue, and the message is distributed and processed by handler, in FIFO execution.

MessageQueue (Message Queuing): Used to hold a message sent through handler, usually attached to a thread that created it, can get the message queue of the current thread through Looper.myqueue ().

Handler: is the main processor of message, responsible for the delivery of message, the execution of message content processing. For example, sending a message to a message queue (sendMessage), updating the UI component (implementing the Handlemessage method of the Class)

Looper: Is the communication bridge between the handler and the message queue, and the program component first passes the message to the Looper,looper by handler and puts the message into the queue. Looper also broadcasts messages from the message queue to all looper.

Message: The types of messages, understood as information exchanged between threads, processing data background threads need to update the UI, in the handler class in the Handlemessage method to get a single message to be processed.

The general flow of the handler mechanism:

1. After the Looper.loop () method runs, the loop takes out the non-null message inside the MessageQueue in the order in which it was received.

2. At the beginning, the message inside the MessageQueue is null, when handler.sendmessage (message) to MessageQueue, This function sets the target property of the message object to be the current handler object. The message is then processed by the DispatchMessage function of the handler called by the target of the message, which is then removed by Looper. In the DispatchMessage method, how to handle a message is specified by the user, three judgements, and priority from high to Low:

1) message inside the callback, an object that implements the Runnable interface, where the run function does the processing work

2) handler inside the Mcallback point to a realization of the callback interface object, by its handlemessage processing.

3) Processing the message handler object corresponding to the class inheritance and implement the Handlemessage function, through the implementation of the Handlemessage function to process the message.

4) When handler finishes processing the message, Looper sets the message to NULL for recycling.

Android face questions please explain the relationship between message, Handler, MessageQueue, Looper in the single-threaded model

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.