A summary of handler in Android

Source: Internet
Author: User

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 opens 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 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 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. At 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 uses the Sedmessage () method to transmit the message object, which contains data, which is placed in the main thread queue and updated with the main thread.

TwoHandler some features
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 in 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 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

1  Public classMyhandleractivityextendsActivity {2 button button;3 MyHandler MyHandler;4 5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.handlertest);8 9button =(Button) Findviewbyid (R.id.button);TenMyHandler =NewMyHandler (); One         //When a new handler instance is created, it is bound to the current thread and message queue and begins distributing the data A         //Handler has two functions, (1): Timed execution of message and Runnalbe object -         //(2): Let an action be executed in a different thread. -  the         //It arranges messages, using the following methods -         //Post (Runnable) -         //postattime (Runnable,long) -         //postdelayed (Runnable,long) +         //sendemptymessage (int) -         //sendMessage (Message); +         //sendmessageattime (Message,long) A         //sendmessagedelayed (Message,long) at        -         //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 (); in     } -  to     /** + * Accept messages, process messages, and this handler will run with the current main thread -     * */ the  *     classMyHandlerextendsHandler { $          PublicMyHandler () {Panax Notoginseng         } -  the          PublicMyHandler (Looper L) { +             Super(L); A         } the  +         //subclasses must override this method to accept data - @Override $          Public voidhandlemessage (Message msg) { $             //TODO auto-generated Method Stub -LOG.D ("MyHandler", "Handlemessage ..."); -             Super. Handlemessage (msg); the             //The UI can be updated here -Bundle B =Msg.getdata ();WuyiString color = b.getstring ("Color"); theMyhandleractivity. This. Button.append (color); -  Wu         } -     } About  $     classMyThreadImplementsRunnable { -          Public voidrun () { -  -             Try { AThread.Sleep (10000); +}Catch(interruptedexception e) { the                 //TODO auto-generated Catch block - e.printstacktrace (); $             } the  theLOG.D ("Thread .....", "Mthread ...")); theMessage msg =NewMessage (); theBundle B =NewBundle ();//Storing Data -B.putstring ("Color", "my"); in Msg.setdata (b); the  theMyhandleractivity. This. Myhandler.sendmessage (msg);//send a message to handler to update the UI About  the         } the}

A summary of handler in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.