Asynchronous message processing mechanism of Android development handler

Source: Internet
Author: User

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

1.Message.

Messages that are passed between threads can carry a small amount of information inside to exchange data between different threading. You can use the What field, Arg1 and arg2 carry some integer data, and the obj field carries a single Object object.

2.Handler.

Handler, which the processor means, is primarily used to send and process messages. Sending a message typically uses the handler SendMessage () method, and the emitted message is eventually passed to the handler Handlermessage () method after a series of tossing and processing.

3.MessageQueue.

Message Queuing, which is used primarily to store all messages sent through handler, is kept in the message queue, waiting to be processed, with only one MessageQueue object in each thread.

4.Looper.

Looper is the steward of messsagequeue in each thread, and after calling the Looper loop () method, it goes into a wireless loop, and then whenever a message is found in MessageQueue, it is removed. and passed to the handler Handlermessage () method. Only one Looper object will exist in each thread.

The process for processing asynchronous messages using handler is as follows:

1. Create a handler object in the main thread and override the Handlermessage () method.

2. When a UI update operation is required in a child thread, a message object is created and sent through the SendMessage () method of the Handler object

3. Messages sent out are added to the queue of MessageQueue waiting to be processed.

4.Looper will always try to remove messages waiting to be processed from messagequeure and distribute them to Handler's Handlermessage () method.

5. Because handler is created in the main thread, the code in the Handlermessage () method is also run in the main thread to update the UI action.

Asynchronous message processing mechanism of Android development handler

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.