In Android, Handler can be used to update the changes of UI in the main thread. The UI can only be updated in the main thread. In order to allow other threads to control UI changes, Android provides a mechanism for Handler, Looper and MessageQueue to work together to achieve the purpose of updating UI in other threads. Generally, we define a Handler in the main thread by the following method
Looper and MessageQueue are not usually seen, so where are they called and how do they collaborate? Looper is not called explicitly in the main thread but is called by default in the ActivityThread.main method.
As shown in the above code, the Looper.prepareMainLooper() method is called to create a Looper in the main thread. If you don’t believe it, let’s check what this method does. Looper prepare
In the prepareMainLooper method, prepare is called. Through prepare, you will find that it actually creates a Looper and assigns it to sThreadLocal. At the same time, you can get the Looper in the current thread through the myLooper method. Let's take a look at what new Looper(quitAllowed) initializes
Here we finally see MessageQueue, which creates a MessageQueue. This message queue is used to save subsequent Messages. Going back to the ActivityThread.main method, we find that it calls Looper.loop() to start the Looper loop and listen to the messages in the message queue MessageQueue. loop Let's look at the source code of Looper.loop():
In the loop, we first get the Looper of the current thread, and also get the MessageQueue in the Looper, which means that the Looper has been bound to the current thread. Then we start a for infinite loop, and find that it is constantly taking messages from the message queue, and finally passing them to msg.target to call its dispatchMessage method. So what is target? Let's go into Message Message
We find that it is the Handler we are familiar with, which means that the dispatchMessage method in the Handler is called to distribute the message. In this way, the Handler contacts the thread bound to the Looper through the Looper, which is the main thread. Handler
Through the initialization of Handler, it obtains the Looper of the thread it is in, and also obtains the message queue in the Looper. Of course, if the Looper of the thread is empty, an exception will be thrown, which explains why when creating a Handler in a non-main thread, you need to call Looper.prepare and Looper.loop respectively, but the main thread does not need to, because it has been called by default. dispatchMessage
Back to the front, for the processing of dispatchMessage, first determine whether msg.callback is empty. Here, callback should be known as a Runnable through the above Message. If it is not empty, directly call the run method of Runnable. Otherwise, call the handleMessage method of Handler. I believe everyone is already familiar with this method. The processing of events is performed in this method. Because we already know that the Handler has contacted the main thread, the processing in handleMessage is naturally relative to the main thread, and the UI can be updated naturally. Through this, we can compare Looper to a bridge to connect the communication between the thread where the Looper is located and the Handler, and manage the messages in the message queue MessageQueue. So how is the previous Runnable not empty? There are two ways to use Handler. One is to directly create a Handler and rewrite its handleMessage method, and the other can be used through Handler.post(Runnable), so that the processing of events is naturally implemented in the run method. The above describes how the Handler contacts the thread that needs to be operated and how the message is retrieved and processed. Next, let's talk about how the message is put into the MessageQueue in the Looper. sendMessageAtTime There are many ways to send messages through Handler, such as sendMessage, sendEmptyMessage and sendMessageDelayed, but in fact, they all call the sendMessageAtTime method. So let's take a look at the implementation of the sendMessageAtTime method.
And sendMessageAtTime calls the enqueueMessage operation. From the name of this method, you can tell that it is an enqueue operation. enqueueMessage
As expected, queue.enqueueMessage(msg, uptimeMillis) in MessageQueue is directly called to add the message to the message queue. At the same time, this code msg.target = this assigns the current Handler to msg.target, which is the Handler called in the Looper.loop method mentioned earlier. In this way, the message is placed in the MessageQueue, and then the message is taken out through the loop mentioned earlier for corresponding processing, thus forming the entire system for processing messages. This is also the principle of using Handler inside. Well, these are basically the connections between Handler, Looper and MessageQueue. I also drew a simple picture and hope it will be helpful. Summarize Let's summarize the process between them. First, create a Handler. There must be a Looper in the thread where the Handler is located. If it is implemented by default in the main thread, other threads must call Looper.prepare to create a Looper and call Looper.loop to start processing messages. Each Looper has a MessageQueue, which is used to store Messages. The Handler puts the message into the message queue through a series of operations such as post or send.., and the Looper keeps listening to the message processing by starting a continuous loop, constantly taking messages from the MessageQueue, and handing them over to the dispatchMessage of the handler bound to the current Looper for distribution. ***According to the situation, the run method of Runnable or the HandlerMessage method of Handler is called to ***process the message. Other sharing: https://idisfkj.github.io/arc... |
<<: A Preliminary Study on WeChat Mini Programs
>>: Big data prescription for startups | WOT Technology Clinic Second Phase Diagnosis
Most of those who make short videos in self-media...
[[153300]] This is the case with the recent "...
Top 10 Wonders of the Rainforest Chen Zhiheng, Co...
On September 6, 2024, at the press conference on ...
Who would have thought that the starch sausages s...
Each product iteration requires that the new user...
Is it easy to be an agent of Jinzhong Group Buyin...
OneCoin price appreciation in 2020 What is the es...
Please call our 24-hour customer service hotline ...
Community is an area that almost all Internet pro...
Ms. Wang, 30 years old, has recently had frequent...
Many people yearn for poetry and distant places, ...
After three years of fighting the epidemic, weari...
For Li Shufu, who has just become the richest man...
Conversion is the key factor in paid promotion . ...