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)