Android thread asynchronous message processing mechanism (ii)--message, Handler, MessageQueue, and Looper

Source: Internet
Author: User

Analysis of asynchronous message processing mechanism

Asynchronous message processing in Android consists of four parts, message, Handler, MessageQueue, and Looper.

1. Message

A message is an information that is passed between threads, and it can carry a small number of messages inside to exchange data between different threading. In the previous example, the What field of message was used, in addition to using the Arg1 and Arg2 fields to carry some shaping data, using the Obj field to carry an object.

2, Handler

Handler, as the name implies, is the meaning of the processor, which is primarily used to send and process messages. Sending a message is typically using the handler SendMessage () method, and the emitted message is eventually passed to the handler Handlemessage () method after a series of tossing and processing.

3, MessageQueue

MessageQueue is the meaning of Message Queuing, which is primarily used to store all messages sent through handler. This part of the message will remain in the message queue and wait for it to be processed. There will only be one MessageQueue object in each thread.

4, Looper

Looper is the steward of MessageQueue in each thread, and after calling the Looper loop () method, it goes into an infinite loop, and then whenever a message is found in MessageQueue, it is removed, And passed to Handler's Handlemessage () method. There will only be one Looper object in each thread.

After understanding the basic concepts of message, Handler, MessageQueue, and Looper, let's comb through the entire process of asynchronous message processing. You first need to create a handler object in the main thread and override the Handlemessage () method. Then, when UI action is required in a child thread, a message object is created and sent out through handler. The message is then added to the MessageQueue queue for processing, and Looper tries to remove the pending message from MessageQueue and finally distributes it back to the Handlemessage () method of handler. Since handler is created in the main thread, the code in the Handlemessage () method will also run in the main thread, so we can do the UI operation safely here.

The following several blog writing is also good, recommended, there is no time to summarize in depth

http://blog.csdn.net/a254373829/article/details/6933027

http://blog.csdn.net/nanzhiwen666/article/details/8292981

Http://www.cnblogs.com/shirley-1019/p/3557800.html

Http://www.cnblogs.com/net168/p/4082217.html

Android thread asynchronous message processing mechanism (ii)--message, Handler, MessageQueue, and Looper

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.