Response to the Handler mechanism:
When it comes to the handler mechanism, it is necessary to design 5 classes (drawing), handler, MessageQueue, Looper, Thread, and a Message; Message is a Message that is uniformly queued by MessageQueue and processed by Handler. Handler is a Handler that sends and processes Message messages. MessageQueue refers to the MessageQueue, which is used to hold the queue sent by the Handler and is executed on a first-in, first-out basis. Looper acts like a pump to pump water out of a MessageQueue. Thread Is the place where the message loop is executed. Before creating an Activity, load the ActivityThread class when the system starts. In the main function of this class, Call stars. Prepare MainLooper initialized () which objects, and then create the main thread of the handler object, then create ActivityThread object, finally call the stars. The loop () method, constant polling messages in the queue. In other words, Looper’s loop() method is enabled before the ActivityThread and Activity are created, constantly polling for messages. We can graphically illustrate how the handler mechanism works: After we prepare Message data by message.obtain (), sendMessage() is used: When sending a message, label the message sent by the Handler with message.target=this. 3. Start the polller in the run method of the HandlerThread class: EnqueueMessage (Message MSG, long WHEN) in MessageQueue That is, the message is queued according to the incoming time. In looper.loop (), the MessageQueue object is retrieved and the Message (Message MSG = queue.next()) is retrieved from it. If there is no Message, the Message will be blocked. From the message queue, call MSG. Target. DispatchMessage (MSG); 9. Send the processed messages to the specified handler. That is, call the dispatchMessage(MSG) method of the Handler to send messages. Recycle the message: After the message is used, call msg.recycle() in the looper.loop () method to recycle the message, that is, restore all the fields of the message to the original state.