< a > handler definition:
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 opens a main thread (that is, the UI thread), the main thread is the UI control in the management interface, and the event is distributed, for example, if you click on a button, Android will distribute the event to the button to respond 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 phenomenon, if 5 seconds has not been completed, will receive an error message (ANR) "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 runs 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 to pass over ( The child thread passes the message object with the Sedmessage () method, which contains data, and puts the messages into the main thread queue, updating the UI with the main thread.
< two > handler some features
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 has two functions: (1): Arranges message messages or Runnable to execute somewhere in the main thread, (2) to have an action executed in a different thread
1 some ways to distribute messages in handler2Post (Runnable)//The post is immediately inserted into the message queue and is run when the message is processed3Postattime (Runnable,Long)4Postdelayed (RunnableLong)5Sendemptymessage (int)6 sendMessage (Message)7Sendmessageattime (Message,Long)8Sendmessagedelayed (Message,Long)
The above method, which starts with post, allows you to arrange a runnable object into the main thread queue, starting with the method of send, allowing you to schedule a message object with data in the queue for updates.
< three > Handler instances
(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
1 Public classMyhandleractivityextendsActivity {2 button button;3 MyHandler MyHandler; 4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.handlertest);7button =(Button) Findviewbyid (R.id.button);8MyHandler =NewMyHandler ();//When a new handler instance is created, it is bound to the queue that creates his current thread and the message (typically on the main thread) and begins distributing the data9 TenMyThread m =NewMyThread ();//instantiating a Runnable object One NewThread (M). Start ();//instantiate a thread object and start a thread to send a message to handler A } - /** - * Accept messages, process messages, and this handler will run with the current main thread the * */ - classMyHandlerextendsHandler { - PublicMyHandler () {} - PublicMyHandler (Looper L) {Super(L);} + - //subclasses must override this method to accept and process the data + @Override A Public voidhandlemessage (Message msg) { at Super. Handlemessage (msg); - //The UI can be updated here -Bundle B =Msg.getdata (); -String color = b.getstring ("Color"); -Myhandleractivity. This. Button.append (color); - } in } - classMyThreadImplementsRunnable { to Public voidrun () { + Try { -Thread.Sleep (10000); the}Catch(interruptedexception e) { * e.printstacktrace (); $ }Panax NotoginsengLOG.D ("Thread .....", "Mthread ...")); -Message msg =NewMessage (); theBundle B =NewBundle ();//Storing Data +B.putstring ("Color", "my"); A Msg.setdata (b); theMyhandleractivity. This. Myhandler.sendmessage (msg);//send a message to handler to update the UI + } - } $}
Example Links:
http://byandby.iteye.com/blog/832467
http://byandby.iteye.com/blog/832915
(Transferred from: http://blog.csdn.net/jincf2011/article/details/6603918)
Android Handler detailed Usage Method Example
Http://www.jb51.net/article/43360.htm
Handler Summary of Android