Android Learning Notes

Source: Internet
Author: User

1.Handler

Function: The main thread is to receive data from the child threads to update the main thread (UI). The primary way to receive data is the message object and the Runnalbe object

Description: Handler has two functions, (1): Timed execution of message and Runnalbe object

(2): Let an action be executed in a different thread.

        It arranges the message, using the following method post (Runnable)
Causes the Runnable R to is added to the message queue. The runnable'll be run on the thread to which this handler is attached.
        
Postattime (Runnable,long) postdelayed (runnable,long) sendemptymessage (int) sendMessage (Message); Sendmessageattime (Message,long) sendmessagedelayed (Message,long)
1  Public classMyhandleractivityextendsActivity {2 button button;3 MyHandler MyHandler;4 5protected voidonCreate (Bundle savedinstancestate) {6 Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.handlertest);8 9button =(Button) Findviewbyid (R.id.button);TenMyHandler =NewMyHandler (); One//When a new handler instance is created, it is bound to the current thread and message queue and begins distributing the data A         //Handler has two functions, (1): Timed execution of message and Runnalbe object -         //(2): Let an action be executed in a different thread. -  the         //It arranges messages, using the following methods -         //Post (Runnable) -         //postattime (Runnable,long) -         //postdelayed (Runnable,long) +         //sendemptymessage (int) -         //sendMessage (Message); +         //sendmessageattime (Message,long) A         //sendmessagedelayed (Message,long) at         -         //the above method starts with post allowing you to process the Runnable object -         //sendMessage () allows you to process a Message object (the message can contain data) -  -MyThread m =NewMyThread (); -NewThread (M). Start (); in     } -  to/** + * Accept messages, process messages, and this handler will run with the current main thread -      * */ the  *classMyHandlerextendsHandler { $ PublicMyHandler () {Panax Notoginseng         } -  the PublicMyHandler (Looper L) { +Super(L); A         } the  +//subclasses must override this method to accept data - @Override $ Public voidhandlemessage (Message msg) { $//TODO auto-generated Method Stub -LOG.D ("MyHandler", "Handlemessage ..."); -Super. Handlemessage (msg); the//The UI can be updated here -Bundle B =Msg.getdata ();WuyiString color = b.getstring ("Color"); theMyhandleractivity. This. Button.append (color); -  Wu         } -     } About  $classMyThreadImplementsRunnable { -  Public voidrun () { -  -Try { AThread.Sleep (10000); +}Catch(interruptedexception e) { the //TODO auto-generated Catch block - e.printstacktrace (); $             } the  theLOG.D ("Thread .....", "Mthread ...")); theMessage msg =NewMessage (); theBundle B =NewBundle ();//Storing Data -B.putstring ("Color", "my"); in Msg.setdata (b); the  theMyhandleractivity. This. Myhandler.sendmessage (msg);//send a message to handler to update the UI About  the         } the     } the  +}
View Code

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.