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 app starts, Android first opens amain thread (i.e. UI thread), the main thread for the UI controls in the management interface for event distribution, for example, if you click on a button, Android will distribute the event to the button, in response to your action. 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, if 5 seconds has not been completed, you will receive an Android system error message Force Close. This time weneed 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, becausehandler running in 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 to pass through (the child thread uses the Sedmessage () method to transmit the younger brother) the Message object, (contains the data), puts these messages in the main thread queue, Updates the UI with the main thread.
Ii. Some features of handler
handler can distribute message objects and runnable objects into the main thread, each handler instance is bound to create his line approached (typically located 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)
AbovePost class MethodAllow youarranges a Runnable object into the main thread queue,
The SendMessage class method allows you to schedule a message object with data in the queue, waiting for updates.
Iii. examples of Handler
(1) Subclasses need to inherit the handler 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 class Myhandleractivity extends Activity {
Button button;
MyHandler MyHandler;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.handlertest);
Button = (button) Findviewbyid (R.id.button);
MyHandler = new MyHandler ();
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 = new MyThread ();
New Thread (M). Start ();
}
/**
* Accept messages, process messages, and this handler will run with the current main thread
* */
Class MyHandler extends Handler {
Public MyHandler () {
}
Public MyHandler (Looper L) {
Super (L);
}
Subclasses must override this method,Accept Data
@Override
public void Handlemessage (Message msg) {
TODO auto-generated Method Stub
LOG.D ("MyHandler", "handlemessage ...");
Super.handlemessage (msg);
//The UI can be updated here
Bundleb= Msg. GetData ();
String color =b.getstring ("color");
MyHandlerActivity.this.button.append (color);
}
}
Class MyThread ImplementsRunnable{
public void Run () {
try {
Thread.Sleep (10000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
LOG.D ("Thread ......", "Mthread ...");
Message msg = new Message ();
Bundle B = new bundle ();//Store data
b.putstring ("Color", "my");
Msg.setdata (b);
MyHandlerActivity.this.myHandler.sendMessage (msg);//Send messages to handler, update UI
}
}
Citation: http://www.open-open.com/lib/view/open1335967054421.html
Android--handler Summary