The countdown implementation in Android

Source: Internet
Author: User

------You need to work hard, you are not qualified to rest-----
Anroid in the countdown implementation:
In this first described the younger brother understand, and then add the finishing. Excuse me
1. Timer and TimerTask
public class TimerTask extends Activity {   //Set a countdown   private int Reclen =12;   Private TextView TextView;  Timer timer=new timer ();  public void OnCreate (Bundle savedinstancestate) {       super.oncreate (savedinstancestate);      Setcontentview (r.layout.timertask);     textview= (TextView) Findviewbyid (r.id.txttime);         After a delay of 1000 milliseconds to start execution, the second 1000 is how long the interval executes     timer.schedule (task,1000,1000);//timertask   }   timertask task= New TimerTask () {public      void run () {      ///jump to the main thread with the      runonuithread (newly Runnable () {//ui thread        public void Run () {            Reclen--;             Textview.settext ("+reclen");              Determine if the countdown is complete if                 (reclen<0) {                     //Stop Execution                  timer.cancel ();                Hide Countdown                textview.setvisibility (View.gone);}}           );  }}}     

2, TimerTask and handler (without timer of the improved type)

    &NBSP;

public class TimerTask extends Activity {//Set a countdown private int reclen = 12;  Private TextView TextView;  Timer timer=new timer ();      public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     Setcontentview (R.layout.timertask);         textview= (TextView) Findviewbyid (r.id.txttime); After a delay of 1000 milliseconds to start execution, the second 1000 is how long the interval executes timer.schedule (task,1000,1000); TimerTask} final Handler handler=new Handler () {public void Handlemessage (Message msg) {Swit           CH (msg.what) {case 1:textview.settext ("" +reclen);                Determine if the countdown is complete if (reclen<0) {//Stop execution timer.cancel ();                   Hidden Countdown textview.setvisibility (View.gone);           }}}} TimerTask task=new TimerTask () {public void run () {            reclen--; Send a message to handler for processing messageMessage=new Message ();                Message.what=1;           Handler.sendmessage (message); }      } }

  




}
       ----------younger brother Talent is not fine, if there is a mistake. Please enlighten me------   

The countdown implementation in Android

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.