Android Countdown feature implementation (Countdowntimer)

Source: Internet
Author: User

Before programming, encountered the countdown function, often write their own, but in fact, Android has helped encapsulate a countdown class Countdowntimer, in fact, the creation of the background thread and handler queue encapsulation became a convenient class call.

Description
Countdowntimer timer = new Countdowntimer (30000, 1000), the first parameter represents the total time, and the second parameter represents the time interval.
This means that the method Ontick is called back every second, and then the OnFinish method is recalled after 30 seconds.


Package Com.androidcountdowntimer2;import Android.app.activity;import Android.os.bundle;import Android.os.countdowntimer;import android.widget.textview;/** * Android countdown feature implementation (Countdowntimer) * @author Zhongyao * * *      public class Mainactivity extends Activity {private MyCount MC;      Private TextView TV;          @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);          Setcontentview (R.layout.activity_main);          TV = (TextView) Findviewbyid (r.id.show);          MC = new MyCount (30000, 1000);      Mc.start (); }/* Defines a countdown internal class */class MyCount extends Countdowntimer {public MyCount (long Millisinfuture, Long Co             Untdowninterval) {super (millisinfuture, countdowninterval); } @Override public void OnTick (long millisuntilfinished) {Tv.settext ("             Please wait: "+ millisuntilfinished/1000 +" seconds ... "); } @OvErride public void OnFinish () {Tv.settext ("Finish");          }} @Override protected void OnDestroy () {Super.ondestroy ();      Mc.cancel (); } }


Android Countdown feature implementation (Countdowntimer)

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.