The principle analysis of "reprint" Android's Handler mechanism realization

Source: Internet
Author: User

Handler is a must-have technology in Android development, but many people are stuck in the use stage. It is easy to use, on two steps, to rewrite Handler's Handlemessage () method on the main thread to send messages on the worker thread. But has anyone ever thought about how this technology is implemented? Let's explore the next.

First of all, let everyone understand the following handler mechanism:


Handler mechanism example diagram

Altogether above appeared several kinds, activitythread,handler,messagequeue,looper,msg (Message), which gave a brief introduction to these classes:

Activitythread: The startup entry of the program, why introduce this class, because the class is the main thread that we say, it operates on the Looper.

Handler: Literally a manipulator, the class has a more important place to send messages (SendMessage) to MessageQueue and manipulate controls (Handlemessage) through Handler. Handler holds the object of this MessageQueue and Looper below.

MessageQueue: literally means Message Queuing, which is the encapsulation message class. Insert and remove a message.

Message: This class is encapsulated with the message body and sent to MessageQueue, the class is implemented through a linked list, and its benefits facilitate MessageQueue insertion and removal operations. There are also some fields (int what,object obj,int arg1,int arg2). What is a user-defined message and code so that the recipient (handler) knows what this is about. obj is used to transfer arbitrary objects, and arg1 and arg2 are used to pass some simple integer types.

Below, we follow the boot sequence for source code analysis:

As can be seen from the above, the Activitythread class is used to start Android, the source code is:


Activitythread class:

Next, we see the Looper class, we go inside to see the source code implementation:

First, let's look at what fields are inside:

Internal properties of the Looper

And then we can't wait to see the Preparemainlooper method, what did we do?

Looper.preparemainlooper () method

Here we can see that preparemainlooper is in order to set up a looper that holds the message queue and message sequencer into ThreadLocal. Now let's look at the Loop method:

Looper.loop () method

We can see that the loop method takes out the internal message sequencer and iterates through the messages, distributing the message based on the target of the message (into the Handlemessage method). If you have questions, you should be unclear why the Looper MessageQueue have a message. Then we'll go and see exactly where the message is added. In other words, it seems that I have not analyzed the handler related to our work. Because you and I both know that handler's role is SendMessage and handlemessage, so we know that the message body of the message sequencer in Looper is definitely added from the SendMessage. No ink, we immediately into the handler source analysis.

First, let's look at the fields of handler:

Fields for Handler

Next, we look at the construction of handler, and we can see that there are two types of construction methods for handler (don't look at 6, they are all called to both methods):

The construction method of handler

Next, we're going to go into the Handler.dispatchmessage () method, because we're going to explain the method above just looper.loop. DispatchMessage method is very simple, only three directions, the source code is:

Handler.dispatchmessage () method

So far, the execution code is over. So here's the question, where did the news come from? With this question, we immediately enter the Handler.sendmessage () logic to see, its source code is:

Handler.sendmessage () method

Finally found the message to send the logic and understand, but also to Shell, in the MessageQueue analysis, first, we review, the message sequencer is initialized in the Looper.prepare (). MessageQueue source code, the construction method is very simple:

MessageQueue construction Method

And then we get to the Messagequeue.enqueuemessage () method to see the source code:

Messagequeue.enqueuemessage () method

This is the final execution code that sends the message, which is to put the message into the message sequencer. In the Looper.loop () method, we need to constantly remove messages from the message sequencer. The process is also we can go in the source of Messagequeue.next () to see:

Messagequeue.next () method

In this way, the whole process is complete. In these executions, the message is their object. We can look at the structure of the message:

Field of message

In addition, the data structure of the message is based on the linked list, the method is very simple, I do not post it.

Summing up, in fact, is to use a threadlocal to store objects, and then at the time of execution, to ensure that the object is not deformed, so that the main thread to update the UI.

The principle analysis of "reprint" Android's Handler mechanism realization

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.