Android Study Notes handler (1)

Source: Internet
Author: User

Handler is a thread communication tool in the Android operating system, and the package is Android. OS. Handler.

There are two queues bound to handler. One is a message queue and the other is a thread queue. Handler can use these two queues to separate:

    1. Send, accept, and process messages-Message Queue;
    2. Start, end, and sleep threads-thread queue;

In Android OS, after a process is created, the main thread (which can be understood as the current activity) creates a message queue, which maintains all top-level application objects (activities, broadcast receivers, etc) and the window created by the main thread. You can create new threads in the main thread. All these new threads communicate with the main thread through handler. Communication is implemented by calling the post () method and sendmessage () method of Handler through a new thread. The functions are described as follows:

    1. Post () adds a thread to the thread queue;
    2. Sendmessage () sends a message object to the message queue;

Of course, there are some variants of the post () method, such as postdelayed () and postattime (), which are used to delay sending and timed sending;

Message processing is carried out in the handler object of the main thread. In the specific processing process, the handlemessage (Message MSG) method of handler must be rewritten using the anonymous internal class when the new handler object is used;

Obtain a message from a message queue.

A thread can be added to a thread queue either in the main thread or in the Child thread, but post () must be called through the handler object of the main thread ().

Package Mars. barhandler; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. OS. message; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. progressbar; </P> <p> public class testbarhandler extends activity {<br/>/** called when the activity is first created. */<br/> // declare the control variable <br/> progressbar bar = NULL; <br/> button startbutton = NULL; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> // obtain the object representing the Control Based on the Control ID, and set the listener for the button <br/> bar = (progressbar) findviewbyid (R. id. bar); <br/> startbutton = (button) findviewbyid (R. id. startbutton); <br/> startbutton. setonclicklistener (New buttonlistener (); <br/>}< br/> // when you click the startbutton button, the onclick method of the buttonlistener is executed. <br/> class buttonlistener implements onclicklistener {</P> <p> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> bar. setvisibility (view. visible); <br/> updatebarhandler. post (updatethread ); <br/>}</P> <p >}< br/> // use an anonymous internal class to rewrite the handlemessage method in handler. <br/> handler updatebarhandler = new handler () {</P> <p> @ override <br/> Public void handlemessage (Message MSG) {<br/> bar. setprogress (MSG. arg1); <br/> bundle = MSG. getdata (); <br/> updatebarhandler. post (updatethread); <br/> system. out. println ("test ---->" + bundle. getstring ("test"); <br/>}</P> <p >}; <br/> // Thread class, this class is declared using an anonymous internal class <br/> runnable updatethread = new runnable () {<br/> int I = 0; <br/> Public void run () {<br/> system. out. println ("begin thread" + I); <br/> I = I + 10; <br/> // get a message object, the message class is provided by the Android operating system <br/> message MSG = updatebarhandler. obtainmessage (); </P> <p> // set the arg1 parameter value of the MSG object to I, and use the member variables arg1 and arg2 to transmit the message, the advantage is that the system performance consumption is relatively small <br/> MSG. arg1 = I; <br/> bundle = new bundle (); <br/> bundle. putstring ("test", "test bundle"); <br/> MSG. setdata (bundle); <br/> try {<br/> // set the current display to sleep for 1 second <br/> thread. sleep (1000); <br/>} catch (interruptedexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> // Add the MSG object to the Message Queue <br/> if (I> 100) {<br/> // If I is set to 100, the thread object is removed from handler. <br/> updatebarhandler. removecallbacks (updatethread); <br/> system. out. println (">>>>>"); <br/>}else {<br/> updatebarhandler. sendmessage (MSG); <br/> system. out. println ("<"); <br/>}< br/> }; <br/> class mythread extends thread {<br/> Public void run () {</P> <p >}< br/>}</P> <p>}

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> </P> <p> <progressbar <br/> Android: id = "@ + ID/Bar" <br/> style = "? Android: ATTR/progressbarstylehorizontal "<br/> Android: layout_width =" 200dp "<br/> Android: layout_height =" wrap_content "<br/> Android: visibility = "gone" <br/> <button <br/> Android: Id = "@ + ID/startbutton" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "start"/> <br/> </linearlayout> <br/>

Related Article

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.