Android Handler comprehensive solution

==============[ Process ]==============

[1]: Looper.prepare() will create a Loop instance in this thread, and create and hold a message queue MessageQueue in this thread. One thread corresponds to only one message queue.

[2]: Looper.loop() continuously loops to read messages in MessageQueue, if there is no message, it will block, and if there is a message, it will pass

target.dispathMessage() is sent by Handler to send a message.

[3]: When creating a Handler, the Looper instance of the current thread will be taken out, and the Looper's message queue MessageQueue() will be associated with it.

[4]: When the Handler finally sends a message through sendMessageAtTime(), it will assign the target of the Message to the Handler itself, and then put the message into the MessageQueue of the current thread. When Looper.loop() loops to fetch the message from the message queue, it will The target corresponding to the Message will be found, and then dispatchMessage() is called by the target to distribute the message. In fact, dispatchMessage() is a method of Handler. Eventually, handlerMessage() will be called to send the message to the user. This handlerMessage() is our usual The method of receiving messages overridden when creating the Handler instance.

[5]: The principle of Handler().post() is the same as that of Message, but in the dispatchMessage() method, it will first determine whether the Runnable of the Message is empty. If it is empty, call the handlerMessage method, and execute Runnable if it is not empty. The code inside.

 

==============[ Thinking/Question ]==============

Question 1  : Loop.loop(), read data in an infinite loop. If there is no data, it will block. Why does it not lead to application development?

                  Because the entire Android application runs, many functions are built on the basis of Loop. For example, the callbacks of Activity's life cycle methods are all under the control of Loop.loop(). If the Loop is blocked, the application stops. Loop is responsible for reading the messages from the message queue and making callbacks for the corresponding functions according to the messages. The callback onResume() will call back onResume() of the corresponding Activity. The meaning of loop infinite loop is to constantly take out messages from the message queue to perform corresponding functions. What really causes the stuck is that the processing time of the previous message is too long, causing the next message to not be processed in time. If the message queue is empty, Loop will block there and enter the sleep state. At this time, it is actually a static state of the application without interaction. If a new event is input, such as an Activity from the background back to the foreground. It will wake up the sleeping Loop, read the message from the message queue, and execute the corresponding function.

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Leo_Liang_jie/article/details/90479216