First,the definition of handler:
Mainly accepts the data sent by the child thread, and updates the UI with this data in conjunction with the main thread .
Explanation: When the application starts,Android first turns on a main thread (that is, the UI thread), the main thread is the UI control in the management interface , for event distribution, for example, if you click on a button, Android will distribute the event to the button, To respond to your actions. If you need a time-consuming operation at this time, for example: network read data, or read a large local file, you can not put these operations in the main thread, if you put in the main thread, the interface will appear suspended animation phenomenon, if 5 seconds is not completed, will receive an error "forced shutdown" on the Android system. This time we need to put these time-consuming operations on a sub-thread, because the child threads involve UI updates, the Android main thread is not secure, that is, the update UI can only be updated in the main thread, and the operations in the child threads are dangerous. This time, Handler appeared. To solve this complex problem, because handler is running in the mainline approached (UI thread), it and the child thread can pass the data through the Message object, at this time, the handler undertakes to accept the child thread passed over (the child thread uses the Sedmessage () method to pass the younger brother) The Message object, which contains data, is placed in the main thread queue and updated with the main thread.
Ii. Some features of handler
Handler can distribute the Message object and the Runnable object into the main thread, and each handler instance is bound to create his line approached (typically in the main thread).
It has two functions: (1): Schedule a message or runnable to execute somewhere in a main thread, (2) Schedule an action to execute on a different thread
Some ways to distribute messages in handler
Post (Runnable)
Postattime (Runnable,long)
Postdelayed (Runnable Long)
Sendemptymessage (int)
SendMessage (Message)
Sendmessageattime (Message,long)
Sendmessagedelayed (Message,long)
The Post class method above allows you to arrange a runnable object into the main thread queue,
The SendMessage class method allows you to schedule a message object with data to queue and wait for updates.
Iii. examples of Handler
(1) Subclasses need to inherit the Hendler class and override the Handlemessage (Message msg) method to accept thread data
The following is an instance that implements the function of modifying the contents of the interface button through a thread
Public classMyhandleractivityextendsActivity {button button; MyHandler MyHandler; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.handlertest); Button=(Button) Findviewbyid (R.id.button); MyHandler=NewMyHandler (); //When a new handler instance is created, it is bound to the current thread and message queue and begins distributing the data//Handler has two functions, (1): Timed execution of message and Runnalbe object//(2): Let an action be executed in a different thread. //It arranges messages, using the following methods//Post (Runnable)//postattime (Runnable,long)//postdelayed (Runnable,long)//sendemptymessage (int)//sendMessage (Message); //sendmessageattime (Message,long)//sendmessagedelayed (Message,long)//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 (); } /*** Accept messages, process messages, and this handler will run with the current main thread **/ classMyHandlerextendsHandler { PublicMyHandler () {} PublicMyHandler (Looper L) {Super(L); } //subclasses must override this method to accept data@Override Public voidhandlemessage (Message msg) {//TODO auto-generated Method StubLOG.D ("MyHandler", "Handlemessage ..."); Super. Handlemessage (msg); //The UI can be updated hereBundle B =Msg.getdata (); String Color= b.getstring ("Color"); Myhandleractivity. This. Button.append (color); } } classMyThreadImplementsRunnable { Public voidrun () {Try{Thread.Sleep (10000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } log.d ("Thread ....", "Mthread ..." "."); Message msg=NewMessage (); Bundle b=NewBundle ();//Storing DataB.putstring ("Color", "my"); Msg.setdata (b); Myhandleractivity. This. Myhandler.sendmessage (msg);//send a message to handler to update the UI } }}
Introduction to Handler in Android