Android's messaging mechanism

Source: Internet
Author: User

1. Background

Handler is the upper-level interface of the Android messaging mechanism, and handler can easily switch a task to the thread where handler is located.

One of the functions of handler is to update the UI, sometimes requiring time-consuming I/O operations in a sub-thread, possibly reading a file or accessing the network, and possibly making some changes to the UI when the time-consuming operation is complete, with handler.

The message mechanism of Android mainly refers to the operating mechanism of handler , handler needs the underlying MessageQueue ( Message Queuing ) and Looper (message loop) the support.

Message Queuing uses a single-linked list of data structures to store message lists.

Looper to find out if there are any new messages in the form of an infinite loop. There is a special concept in Looper, ThreadLocal, which is the function of storing data in each thread.

When handler is created, the looper of the current thread is used to construct the message circulatory system, then handler internally is threadlocal to get the looper of the current thread. The thread does not have looper by default, and if you need to use handler, you must create looper for the thread.

The main thread we often mention, also called the UI thread, is that Activitythread,activitythread is initialized when the Looper is created, which is why the handler can be used by default in the main thread.

2. Overview of Android message mechanism

The main function of handler is to switch a task to a specified thread to execute. The reason the system provides handler is to resolve inconsistencies in the inability to access the UI in child threads.

Android provides access to the UI only in the main thread, and if the UI is accessed in a child thread, the program throws an exception.

Android's UI space is not thread-safe

The Viewrootimpl validates the UI operation, which is done by the Viewrootimpl Checkthread method.

A runnable is delivered to the looper inside the Handler via the Handler post method. In fact, the Post method is ultimately done through the Send method.

When the handler send method is called, it calls MessageQueue's Enqueuemessage method to place the message in the message queue, and then Looper discovers that a new message arrives and then processes the message. The Handlemessage method of runnable or handler in the final message is called.

3. How Message Queuing works

Message Queuing on Android is MessageQueue, which consists of two operations: inserting Enqueuemessage and reading next. Insert is the insertion of a message into the message queue, and Next's role is to remove a message from the message queue and remove it from the message queue.

MessageQueue is to maintain a list of messages through a data structure of a single linked list.

Next is an infinite loop, and if there are no messages in the message queue, then the next method will always be blocked here. When a new message arrives, the next method returns the message and removes it from the single-linked list.

4. Working principle of Looper

Looper in the Android message mechanism always plays the role of the message loop, and it will constantly see if there are new messages from the MessageQueue.

Handler's work requires Looper, and no looper thread will get an error. You can create a looper for the current thread through Looper.prepare (), and then turn on the message loop through the Looper.loop () method.

1 Looper.prepare (); 2 New Handler (); 3 looper.loop ();

5. Working principle of Handler

The work of handler mainly includes the sending and receiving process of messages.

The message is sent through a series of methods of Post and a series of methods of Send, the post of a series of methods are finally through the send a series of methods to achieve.

Handler the process of sending a message is simply inserting a message into the message queue, MessageQueue's next method returns the message to Looper,looper and begins processing. The final message is handled by the DispatchMessage method that looper handed to handler.

The meaning of callback is that you can use to create an instance of handler, but you do not need to derive a subclass of handler.

Finally, call handler's Handlemessage method to process the message.

6, the main thread of the message loop

The main thread of Android is Activitythread, the main thread of the entry method is main, in the main method of the system through the Looper. Preparemainlooper () to create the Looper and MessageQueueof the main thread, and to turn on the message loop of the main thread via Looper.loop ().

After the start of the message loop for the main thread, Activitythread also needs a handler to interact with the message queue, which is ActivityThread.H, which defines a set of message types internally. Mainly includes four components of the start and stop process.

Activitythread inter-process communication via Applicationthread and AMS , When AMS completes a activitythread request in the form of interprocess communication, the Binder method in Applicationthread is called back, and Applicationthread sends a message to H. H after receiving the message will switch logic in Applicationthread to Activitythread to execute, that is, switch to the main thread to execute , this process is the main thread of the message loop model .

Android's messaging mechanism

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.