Handler definition: Used to receive the data sent by the Cheng, and use this data to directly update the main thread of the UI.
Android, when an app launches a main thread (UI thread), his responsibility is to manage the controls in the interface . For example, when you click on a button, the UI thread distributes an event to the button, corresponding to your action. If your operation is time-consuming, such as networked operations, the main thread of the program is blocked before the server responds, and the reaction to the user experience is stuck. If more than 5 seconds, the Android system will pop up "forced shutdown", how to do?
In the response operation, a new thread is opened, and the time-consuming operation is performed in the new thread, and the main thread is the interface that is still operational. The problem is solved, but there is a problem, if the sub-thread operation is completed, how to get the data update interface UI, directly updated? The main thread of Android is threads that are unsafe (update UI must be updated in the main thread)and child threads are not allowed.
This is the time for the savior to come. Handler.
Handler runs in the mainline approached (UI thread), and the child thread can pass the data through the Message object, at which point the handler takes on the Message object (the child thread uses the Sedmessage () method, which is passed over by the child thread), (which contains the data ), put these messages into the main thread queue and update the UI with the main thread.
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)
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
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 } }View Code
Http://www.cnblogs.com/tt_mc/archive/2012/01/30/2331876.html
Http://www.open-open.com/lib/view/open1335967054421.html
Http://www.cnblogs.com/bastard/archive/2012/06/08/2541944.html
Http://www.cnblogs.com/lijunamneg/p/3573093.html
Handle, Runnable, Thread, Message Queuing (MessageQueue)
Android's handle