Android Source Learning (1) Handler

Source: Internet
Author: User

Instantiation of the handler

In Android development, handler is often used to commit a task to a specified thread (such as the main thread) to execute or to delay execution. The handler constructor has several overloaded forms, but eventually it is called to one of the following two types:

 PublicHandler (Callback Callback,Booleanasync) {Mlooper=Looper.mylooper (); if(Mlooper = =NULL) {        Throw NewRuntimeException ("Can ' t create handler inside thread that have not called looper.prepare ()"); } mqueue=Mlooper.mqueue; Mcallback=callback; Masynchronous=Async;} PublicHandler (Looper Looper, Callback Callback,Booleanasync) {Mlooper=Looper; Mqueue=Looper.mqueue; Mcallback=callback; Masynchronous=Async;}

As you can see, the difference between these two constructors is that Looper is not obtained in the same way that when Looper is not specified, handler internally gets the looper of the current thread through the Looper.mylooper () static method. The message or runnable that we send to handler is handled by this looper (or the thread of the Looper). We can get the looper of the main thread through Looper.getmainlooper ().

Send a message or runnable

Call post, Postattime, postdelayed commit runnable, call SendMessage, Sendemptymessage, sendmessagedelayed, Sendemptymessagedelayed, Sendmessageattime, Sendemptymessageattime send a message, which is eventually called to Sendmessageattime:

1  Public BooleanSendmessageattime (Message msg,LongUptimemillis) {2MessageQueue queue =Mqueue;3     if(Queue = =NULL) {4RuntimeException e =NewRuntimeException (5                  This+ "Sendmessageattime () called with no Mqueue");6LOG.W ("Looper", E.getmessage (), e);7         return false;8     }9     returnenqueuemessage (Queue, MSG, uptimemillis);Ten } One  A Private BooleanEnqueuemessage (MessageQueue queue, Message msg,LongUptimemillis) { -Msg.target = This; -     if(masynchronous) { theMsg.setasynchronous (true); -     } -     returnqueue.enqueuemessage (msg, uptimemillis); -}

That is, the submitted runnable will eventually be wrapped into a message, and the callback field of the message will point to the runnable. All submitted message will be inserted into an ordered queue named Mqueue, sorted by the incoming Uptimemillis (the value is assigned to the When field of the message in the Enqueuemessage operation). MessageQueue uses producer-consumer patterns, post* and send* to generate messages that will eventually be consumed by Looper. (The 2nd line assigns mqueue to the local variable queue and then by changing the access Mqueue, apparently out of multithreading concurrency security Considerations)

With Postatfrontofqueue, sendmessageatfrontofqueue you can submit runnable and message to Mqueue's team header separately, The runnable or message will be processed in the next loop of the Looper.

Use Removecallbacks, Removemessages, removecallbackandmessages to remove the runnable or message that has been submitted.

Android Source Learning (1) Handler

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.