Android Handler role

Android handler role:

what is a handler? The handler plays the role of adding messages to MQ and processing messages (only processing messages sent by itself), that is, notifying MQ that it wants to perform a task (sendMessage), and executing the task (handleMessage) when the loop reaches itself. The process is asynchronous. When the handler is created, it will be associated with a looper. The default constructor will be associated with the looper of the current thread, but this can also be set.
A thread can have multiple Handlers, but only one Looper!
With the handler, we can use post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long) and sendMessageDelayed(Message, long) ) These methods send messages to MQ. Just looking at these APIs, you may think that the handler can send two kinds of messages, one is the Runnable object and the other is the message object. This is an intuitive understanding, but in fact, the Runnable objects sent by the post are finally encapsulated as message objects

1. A handler can send messages on any thread, and these messages will be added to the associated MQ.
2.handler processes messages in its associated looper thread.
Of course, handler can do much more than that. Because it can post Runnable objects, it can also cooperate with Looper to implement the classic Pipeline Thread (pipeline thread) mode. Please refer to this article "Android Guts: Intro to Loopers and Handlers"

For Handler post Runnable objects:
Handler mHandler=new Handler();
     mHandler.post(new Runnable(){
        @Override public void run()
        { // TODO Auto-generated method stub
         }
     });


The official explanation of this method is as follows, pay attention to it: "The runnable will be run on the user interface thread. "

View.post(Runnable) method. In the post(Runnable action) method, the View obtains the Handler of the current thread (ie, the UI thread), and then posts the action object to the Handler. In the Handler, it wraps the passed action object into a Message (the callback of the Message is action), and then puts it into the message loop of the UI thread. When the Handler processes the Message again, there is a branch (the unexplained one) that is set for it, and the run method of the runnable is called directly. At this point, it has been routed to the UI thread, so we can update the UI without any worries.


Encapsulating task Message
In the entire message processing mechanism, message is also called task, which encapsulates the information carried by the task and the handler that handles the task. The usage of message is relatively simple and will not be summarized here. But there are a few points to note (to be added):

1. Although Message has a public default constructor, you should obtain an empty message object from the message pool through Message.obtain() to save resources.

2. If your message only needs to carry simple int information, please use Message.arg1 and Message.arg2 to transmit information first, which saves more memory than using Bundle

3. Use message.what to identify information so that it can be used in different ways Handling messages.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326942682&siteId=291194637