Simple countdown to Android CountDownTimer
CountDownTimer:
Schedule a countdown until a time in the future, with regular communications on intervals along the way.
The countdown class is relatively simple. You can learn how to design the class. Here is an example of a simple application of this class:
Optimization is also required for such a small example. The optimization has not been considered as a result. In simple optimization, the code for clicking Dlialog multiple times is added without re-creating and canceling the countdowntimer task;
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; privat E CountDownTimer mDownTimer; 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 started", Toast. LENGTH_SHORT ). show (); If (dialog! = Null) {dialog. dismiss () ;}}); mDownTimer = new CountDownTimer (10000,100 0) {public void onTick (long millisUntilFinished) {if (btnSure! = Null) {btnSure. setText ("automatic start (" + millisUntilFinished/1000 + "/s)") ;}} public void onFinish () {if (btnSure! = Null) {btnSure. optional mclick ();}}}. start (); dialog. setContentView (v); dialog. setTitle ("Start game"); dialog. setOnDismissListener (new DialogInterface. onDismissListener () {@ Overridepublic void onDismiss (DialogInterface dialog) {if (mDownTimer! = Null) {mDownTimer. cancel (); mDownTimer = null ;}}); dialog. show ();}}
Only implement countdown.
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 saved InstanceState) {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 started", Toast. LENGTH_SHORT ). show (); if (dialog! = Null & dialog. isShowing () {dialog. dismiss () ;}}); new CountDownTimer (10000,100 0) {// total time, interval public void onTick (long millisUntilFinished) {btnSure. setText ("coming soon (" + millisUntilFinished/1000 + "/s)");} public void onFinish () {if (dialog! = Null & dialog. isShowing () {btnSure. optional mclick (); // click the dialog button. dismiss ();}}}. start (); dialog. setContentView (v); dialog. setTitle ("Start game"); dialog. show ();}}