Countdowntimer:
Schedule a countdown until a time in the future, with regular notifications on intervals along the the.
Countdown This class is relatively simple, you can learn to design the class, below see a simple application of this class example:
This small example also needs to be optimized, the optimization is not considered to have only achieved the effect, simple optimization under the addition of multiple clicks Dlialog do not repeat the creation and cancellation of Countdowntimer task code;
After simple optimization:
Package Com.example.dliagdemo;import Android.app.activity;import Android.app.dialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.os.countdowntimer;import Android.view.layoutinflater;import Android.view.view;import Android.widget.button;import android.widget.Toast; public class Mainactivity extends Activity {private Button btnsure;private mainactivity instance;private countdowntimer m Downtimer;private Dialog Dialog; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); instance = this; Button btn = (button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {if (dialog = = null) ShowDialog (); else if (!dialog.isshowing ()) ShowDialog ();});} private void ShowDialog () {Dialog = New dialog (instance); Layoutinflater Inflater = Layoutinflater.from (instance); View v = inflater.inflate (r.layout.itmes, null); btnsure = (Button) v.finDviewbyid (r.id.buttonsure); Btnsure.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick ( View v) {Toast.maketext (instance, "Game start", Toast.length_short). Show (); if (dialog! = null) {Dialog.dismiss ();}}); Mdowntimer = new Countdowntimer (10000, +) {public void OnTick (long millisuntilfinished) {if (btnsure! = null) {btnsure. SetText ("Auto Start (" + millisuntilfinished/1000+ "/s)");}} public void OnFinish () {if (btnsure! = null) {Btnsure.performclick ();}}}. Start ();d Ialog.setcontentview (v);d ialog.settitle ("Start Game");d Ialog.setondismisslistener (new Dialoginterface.ondismisslistener () {@Overridepublic void Ondismiss (Dialoginterface dialog) {if (Mdowntimer! = null) { Mdowntimer.cancel (); Mdowntimer = null;}}); Dialog.show ();}}
Simply achieve the countdown effect.
Package Com.example.dliagdemo;import Android.app.activity;import Android.app.dialog;import android.os.Bundle; Import Android.os.countdowntimer;import Android.view.layoutinflater;import Android.view.view;import Android.widget.button;import Android.widget.toast;public class Mainactivity extends Activity {private Button btnsure; Private mainactivity instance; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); instance =this; Button btn = (button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {showDialog (); }});} private void ShowDialog () {final Dialog Dialog = new Dialog (instance); Layoutinflater Inflater = Layoutinflater.from (instance); View v =inflater.inflate (r.layout.itmes, null); btnsure = (Button) V.findviewbyid (r.id.buttonsure); Btnsure.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {Toast.maketext (instance , "Game start", Toast.length_short). Show (); if (dialog!=null&&dialog.isshowing ()) {Dialog.dismiss ();}}); New Countdowntimer (10000, 1000) {//Total time, interval public void OnTick (long millisuntilfinished) {Btnsure.settext ("Start soon" + millisuntilfinished/1000+ "/s)");} public void OnFinish () {if (dialog!=null&&dialog.isshowing ()) {Btnsure.performclick ();//Click the button Dialog.dismiss ( );}}}. Start (); Dialog.setcontentview (v);d ialog.settitle ("Start Game");d ialog.show ();} }
Android Countdowntimer countdown Simple to use