android handler problem

1. What is a handler?

Answer: The handler is a mechanism to update the UI interface and a message processing mechanism. We can send messages and process messages.

 

2. Why is there a Handler?

A: When designing Android, it encapsulates a set of message creation, delivery, and processing mechanisms. If you do not follow this mechanism, there is no way to update the UI information, and an exception will be thrown.

 

3. How to use the handler?

答:1、post(Runnable);

       2、postDelayed(Runnable ,long);

       3 、 sentMessage

       4 、 sentMessageDelayed

 

4. Why should Android be set to update the UI only through the Handler mechanism?

Answer: The most fundamental problem is to solve the problem of multi-thread concurrency;

      Suppose if there are multiple threads to update the UI in an Activity, and there is no locking mechanism, will the horse cause such a problem? -Chaotic update interface;

      What kind of problems would arise if all operations for updating the UI are locked? -Performance degradation

      For the consideration of the above problems, Android provides a set of mechanisms for updating the UI. We only need to follow such a mechanism.

      Don't worry about multi-threading. The operations of updating the UI are all polled in the main thread's message queue.

 

5. What is the principle of the handler?

Answer: 1. The handler package message is sent (mainly including to whom the message is sent)

      2. Looper-the carrier of message encapsulation. (1) There is a MessageQueue inside, all the messages sent by the Handler go to this message queue; (2) The Looper.Looper method is an endless loop, constantly fetching messages from the MessageQueue, if there is a message, process the message, if there is no message Blocked.

      3. MessageQueue, a message queue, add messages, process messages

      4. The handler is internally associated with Looper, handler-> Looper-> MessageQueue, the handler sends a message to send a message to the MessageQueue queue.

     Summary: The handler is responsible for sending messages, and Looper is responsible for receiving the messages sent by the handler and passing the messages back to the handler himself.

              MessageQueue is a container for storing messages.

 

6. What is the role of HandlerThread?

Answer: HandlerThread thread = new  HandlerThread ("handler thread"); automatically includes a waiting mechanism, and waits for Looper to be created before creating a Handler to avoid the null pointer exception.

 

7, the main thread

* ActivityThread creates the main thread by default, Looper is created by default in the main, and MessageQueue is created by default by the Looper

* threadLocal saves thread variable information, methods include: set, get

 

8. How does Android update the UI?

Answer: 1. runOnUIThread

   2、handler post

   3、handler sendMessage

   4、view post

 

9. Can non-UI threads really update the UI?

Answer: Not necessarily, the reason why the child thread cannot update the interface is because Android uses checkThread in the thread method to determine whether it is the main thread, and this method is in ViewRootImpl. This class is generated in onResume, so At this time, if the child thread generates an update UI in the onCreate method, and does not block, it is a time-consuming operation, and the UI can still be updated.

 

10. Problems encountered when using Handler?

Answer: For example, the child thread updates the UI because the checkThread method is triggered to check whether the UI is updated in the main thread. There is also no Looper in the child thread. This reason is caused by the mechanism of the Handler. Put the Message in the MessageQueue, and if there is no Looper at this time, the MessageQueue cannot be output in a loop. At this time, the Looper is empty error.

 

11. How does the main thread notify the child thread?

Answer: You can use HandlerThread to generate a Handler of a child thread, and implement the handlerMessage method, and then generate a Handler in the main thread, and then notify the child thread by calling the sendMessage method. Similarly, the child thread can also call the sendMessage method to notify the main thread. The benefits of doing this, such as loading some pictures, accessing the network may be more time-consuming, so it is more appropriate to put it in the child thread.

Published 10 original articles · Like 11 · Visits 20,000+

Guess you like

Origin blog.csdn.net/u013323018/article/details/83019963