Principle analysis of Android handler mechanism

Handler is a technology that must be mastered in Android development, but many people stay in the use stage. It is very simple to use. There are only two steps. Rewrite the handler's handlemessage () method in the main thread and send messages in the working thread. But has anyone thought about how this technology is implemented? Let's discuss it together.

Let's start with the figure above to understand the handler mechanism:

Example diagram of handler mechanism

There are several categories in the above, activitythread, handler, messagequeue, looper, MSG (message). A brief introduction to these categories:

Activitythread: the startup entry of the program. Why introduce this class is because this class is what we call the main thread, which operates on looper.

Handler: it literally means the operator. This class has more important aspects, that is, sending a message (SendMessage) to the messagequeue and updating the operation control (handlemessage) through the handler. The objects holding the messagequeue and looper under the handler.

Messagequeue: literally means message queue, which encapsulates the message class. Insert and remove messages.

Message: this class encapsulates the message body and is sent to the messagequeue. The class is implemented through a linked list. Its benefits facilitate the insertion and extraction of messagequeue. Other fields are (int what, object obj, int arg1, int arg2). What is a user-defined message and code so that the receiver knows what this is about. Obj is used to transfer arbitrary objects, and arg1 and arg2 are used to pass some simple integer types.

Next, we will analyze the source code according to the startup sequence:

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

Activitythread class:

Next, we see the looper class. Let's go in and have a look at the source code implementation:

First, let's look at the fields:

Looper's internal properties

Then we can't wait to see what the preparemainlooper method does

Looper. Preparemainlooper() method

Here we can see that preparemainlooper is used to set a looper holding message queue and message sequencer into ThreadLocal. Next, let's look at the loop method:

Looper. Loop() method

We can see that in the loop method, the internal message sequencer will be taken out, the messages inside will be iterated, and the messages will be distributed according to the target of the messages (into the handlemessage method). If you have questions, you should not know why Looper's messagequeue has a message. So let's go and see where the message is added. In other words, it seems that I haven't analyzed the operations related to our handler here. Because you and I both know that the role of handler is SendMessage and handlemessage, we know that the message body of the message sequencer in looper must be added from SendMessage. No ink, we will immediately enter the source code analysis of handler.

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

Field of handler

Next, let's look at the constructor of handler. We can see that there are two types of constructor for handler (don't see six, they are all called to these two methods):

Construction method of handler

Next, we will enter the handler Dispatchmessage () method, because we want to explain the looper Loop method. The method of dispatchmessage is very simple. There are only three directions. Its source code is:

Handler. Dispatchmessage() method

So far, the execution code is over. So the question is, where did the news come from? With this question, we immediately enter the handler Sendmessage() logic. Its source code is:

Handler. Sendmessage() method

It's not easy to find and understand the logic of sending messages, but it needs to be shelled. It's analyzed in messagequeue. First, let's review that the message sequencer is in looper Initialized in prepare(). Messagequeue source code, the construction method is very simple:

Messagequeue construction method

Then we arrive at messagequeue Look at the source code in enqueuemessage() method:

MessageQueue. Enqueuemessage() method

This is the final execution code for sending the message, that is, putting the message into the message sequencer. In looper In the loop () method, we need to constantly fetch messages from the message sequencer. The process is also that we can enter messagequeue Take a look at the source code of next():

MessageQueue. Next() method

In this way, the whole process is completed. During these executions, message is their object. We can see the structure of message:

Field of message

In addition, the data structure of message is based on linked list. The methods are very simple, so I won't post it.

To sum up, it actually uses a ThreadLocal to store objects, and then when executing, it can ensure that the objects are not deformed, so that the UI can be updated in the main thread first.

The above is the detailed explanation of Android handler. We will continue to sort out relevant materials in the future. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>