First, I declare that the following is what I reprinted and sorted.
Thanks
Http://blog.csdn.net/Android_Tutor/archive/2010/05/08/5568806.aspx
Http://www.pocketdigi.com/20100814/45.html
Sharing these friends!
If we directly put the processing function in oncreate or onstart of activity when processing download or other tasks that require long execution, the entire activity will not respond during execution, if the time is too long, the program will be suspended. Handler puts these functions in a separate thread for execution, which is independent from activity.
The following is an example:
Example 1: update the title (title) in five seconds ),
The Code is as follows:
Package COM. android. tutor; </P> <p> Import Java. util. timer; <br/> Import Java. util. timertask; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. OS. message; </P> <p> public class handlerdemo extends activity {</P> <p> // Title provides variables for the settitle method, here, I set the int type <br/> private int title = 0; </P> <p> private handler mhandler = new handler () {</P> <p> Public void handlemessage (Message MSG) {<br/> switch (MSG. what) {<br/> case 1: <br/> updatetitle (); <br/> break; <br/>}< br/> }; <br/>}; </P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> timer = new timer (); <br/> timer. scheduleatfixedrate (New mytask (), 1, 5000 ); <br/>}</P> <p> private class mytask extends timertask {<br/> @ override <br/> Public void run () {</P> <p> message = new message (); <br/> message. what = 1; <br/> mhandler. sendmessage (Message); </P> <p >}< br/>}</P> <p> Public void updatetitle () {</P> <p> settitle ("welcome to Mr Wei's blog" + title); <br/> title ++; <br/>}< br/>}
Example 2: Use handler to update the progress bar. When you press the start button, the progress bar increases by 1 every 500 milliseconds until it is added to 100 or stopped.
Code:
Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <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 main extends activity {<br/>/** called when the activity is first created. */<br/> progressbar PB1; <br/> handler handle = new handler (); <br/> // create a new handler object <br/> button B1; <br/> button B2; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> PB1 = (progressbar) findviewbyid (R. id. PB1); <br/> pb1.setprogress (0); <br/> b1 = (button) findviewbyid (R. id. b1); <br/> b1.setonclicklistener (b1lis); <br/> b2 = (button) findviewbyid (R. id. b2); <br/> b2.setonclicklistener (b2lis); </P> <p >}</P> <p> private onclicklistener b1lis = new onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> // todo auto-generated method stub <br/> handle. post (ADD); <br/> // start to execute add <br/>}</P> <p >}; <br/> private onclicklistener b2lis = new onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> // todo auto-generated method stub <br/> handle. removecallbacks (ADD); <br/> // stop execution <br/> pb1.setprogress (0); <br/>}</P> <p> }; <br/> int pro = 0; <br/> runnable add = new runnable () {<br/> // define add <br/> @ override <br/> Public void run () {<br/> // todo auto-generated method stub <br/> pro = pb1.getprogress () + 1; <br/> pb1.setprogress (Pro ); <br/> settitle (string. valueof (Pro); <br/> If (PRO <100) {<br/> handle. postdelayed (ADD, 500); <br/> // if the progress is less than 100 ,, after a delay of 500 milliseconds, add <br/>}</P> <p >}; </P> <p>}