Android Handler Summary: androidhandler
<1> Handler definition:
It mainly accepts data sent by sub-threads and uses this data with the master thread to update the UI.
Explanation: When an application is started, Android starts a main thread (that is, the UI thread), which is the UI control in the management interface and distributes events. For example, if you click a Button, Android will distribute the event to the Button to respond to your operation.
If you need a time-consuming operation, such as reading data online or reading a large local file, you cannot put these operations in the main thread, if you put it in the main thread, the interface will be suspended. If it is not completed in five seconds, you will receive an error message (ANR) "force close" from the Android system ". At this time, we need to put these time-consuming operations in a sub-thread. Because the sub-thread involves UI update, the main Android thread is thread insecure, that is, updating the UI can only be updated in the main thread. Operations in the Child thread are dangerous. At this time, Handler emerged to solve this complicated problem. Because Handler runs in the main thread (in the UI thread), it and the sub-thread can transmit data through the Message object, at this time, Handler undertakes to accept the Message object transmitted by the Sub-thread (the Sub-thread uses the sedMessage () method to transmit the Message object, which contains data) and put the messages into the main thread queue, updates the UI with the main thread.
<2> Handler features
Handler can distribute the Message object and Runnable object to the main thread. Each Handler instance is bound to the thread that creates the handler (usually located in the main thread ),
Handler has two functions: (1) Arranging Message or Runnable to execute somewhere in the main thread; (2) letting an action be executed in different threads
1 Some methods for sending messages in Handler 2 post (Runnable) // post is immediately inserted into the message queue. When the message is processed, 3 postAtTime (Runnable, long) is run) 4 postDelayed (Runnable long) 5 sendEmptyMessage (int) 6 sendMessage (Message) 7 sendMessageAtTime (Message, long) 8 sendMessageDelayed (Message, long)
The above method starts with post, allows you to arrange a Runnable object to the main thread queue, the method starts with send, allows you to arrange a Message object with data to the queue, wait for updates.
<3> Handler instance
(1) The subclass must 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 interface Button content through a thread.
1 public class MyHandlerActivity extends Activity {2 Button button; 3 MyHandler myHandler; 4 protected void onCreate (Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. handlertest); 7 button = (Button) findViewById (R. id. button); 8 myHandler = new MyHandler (); // when a new Handler instance is created, will be bound to the queue that creates his current thread and the message (usually located in the main thread), start distributing data 9 10 MyThread m = new MyThread (); // instantiate the Runnable object 11 new Thread (m ). start (); // instantiate the thread object, start the thread, and send the message 12} 13/** 14 * to handler to receive the message and process the message, this Handler runs 15 **/16 class MyHandler extends Handler {17 public MyHandler () {} 18 public MyHandler (low.l) {super (L);} together with the current main thread );} 19 20 // subclass must override this method to accept and process data 21 @ Override22 public void handleMessage (Message msg) {23 super. handleMessage (msg); 24 // you can update UI25 Bundle B = msg here. getData (); 26 String color = B. getString ("color"); 27 MyHandlerActivity. this. button. append (color); 28} 29} 30 class MyThread implements Runnable {31 public void run () {32 try {33 Thread. sleep (10000); 34} catch (InterruptedException e) {35 e. printStackTrace (); 36} 37 Log. d ("thread ....... "," mThread ........ "); 38 Message msg = new Message (); 39 Bundle B = new Bundle (); // store data 40 B. putString ("color", "my"); 41 msg. setData (B); 42 MyHandlerActivity. this. myHandler. sendMessage (msg); // send a message to Handler, update UI43} 44} 45}
Example link:
Http://byandby.iteye.com/blog/832467
Http://byandby.iteye.com/blog/832915
(