What is handler (4)

Source: Internet
Author: User

1. Post (runnable) method of handler

2. runnable Running Mechanism

3. Differences between the POST method and the sendmessage Method

 

1. Post (runnable) method of handler

1 public static class placeholderfragment extends fragment {2 3 private button; 4 private handler = new handler (); // The myhandler Method 5 6 Public placeholderfragment () is not written as before () {7} 8 9 @ override10 public view oncreateview (layoutinflater Inflater, viewgroup container, 11 bundle savedinstancestate) {12 view rootview = Inflater. inflate (R. layout. fragment_main, container, false); 13 14 button = (button) rootview. findviewbyid (R. id. buttonid); 15 button. setonclicklistener (New onclicklistener () {16 @ override17 public void onclick (view v) {18 testthread TT = new testthread (); 19 TT. start (); 20} 21}); 22 23 return rootview; 24} 25 26 class testthread extends thread {27 28 @ override29 public void run () {30 runnable r = new runnable () {31 @ override32 public void run () {33 log. I ("tag", "cuurentthread --->" + thread. currentthread (). getname (); 34} 35}; 36 handler. post (r); 37} 38} 39}

The printed results are amazing.

  

This means that runnable runs in the main thread !!!

Look at the source code, Handler's post Method

1 public final boolean post(Runnable r)2     {3        return  sendMessageDelayed(getPostMessage(r), 0);4     }

First read getpostmessage (r)

1 private static message getpostmessage (runnable R) {2 // This method completes two operations: 1. A message object is generated. 2. Assign the value of R to the callback attribute of Message 3 message m = message. Obtain (); 4 M. Callback = r; // The callback attribute of message 5 return m; 6}

Therefore, the POST method of handler is equivalent

1 public final boolean post(Runnable r)2     {3         //return sendMessageAtTime(getPostMessage(r), 0);4         Meesage msg = getPostMessage(r);5         return sendMessageDelayed(msg, 0);6     }

In the above example, what does sendmessagedelayed do?

 1      * Enqueue a message into the message queue after all pending messages 2      * before (current time + delayMillis). You will receive it in 3      * {@link #handleMessage}, in the thread attached to this handler. 4      *   5      * @return Returns true if the message was successfully placed in to the  6      *         message queue.  Returns false on failure, usually because the 7      *         looper processing the message queue is exiting.  Note that a 8      *         result of true does not mean the message will be processed -- if 9      *         the looper is quit before the delivery time of the message10      *         occurs then the message will be dropped.11      */12     public final boolean sendMessageDelayed(Message msg, long delayMillis)13     {14         if (delayMillis < 0) {15             delayMillis = 0;16         }17         return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);18     }

That is, delayed sending. When delaymillis = 0, it is equivalent to direct sending.

In summary: generate a message object, assign R to the callback attribute of the message object, and then place the message object in the message queue.

Logoff retrieves the message object carrying the r object, calls the dispatchmessage method to determine whether the callback attribute of the message has a value. At this time, the callback attribute has a value,

Therefore, handcallback (Message MSG) is executed, and MSG. Callback. Run () is executed in this method ();

 

Testthread retrieves data from the network, defines a runnable object, and directly writes the code for updating the UI in its run () method,

The handler generated in the main thread is used for post. The message object is generated in the POST method, and R is assigned to the callback attribute of meesage,

The message is put into the message queue, logoff is taken out, and the dispatchmessage method is called,

Call the handlercallback method in dispatchmessage. the runnable run () method is executed in handlercallback.

Because logoff is in the main thread, so dispatchmessage and handlercallback will also be in the main thread. The same is true for the run () method.

 

What is handler (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.