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