Android-Countdown implementation
CountDownTimer class implements the countdown function. Encapsulate background thread creation and Handler queue into a convenient class call. This class is relatively simple and has only four methods: onTick, onFinsh, cancel, and start. The first two are abstract methods, So rewrite them. The following is a small official example: copy the Code new CountdownTimer (30000,100 0) {public void onTick (long millisUntilFinished) {mTextField. setText ("seconds remaining:" + millisUntilFinished/1000);} public void onFinish () {mTextField. setText ("done! ");}}. Start (); copy the Code to copy the Code package com. yydcdut. daojishi; import android. OS. bundle; import android. OS. countDownTimer; import android. app. activity; import android. view. menu; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private MyCount mc; private TextView TV; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedIn StanceState); setContentView (R. layout. activity_main); TV = (TextView) findViewById (R. id. show); mc = new MyCount (30000,100 0); mc. start () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;}/* defines an internal countdown class */class MyCount extends CountDownTimer {public MyCount (Long millisInFuture, long countDownInterval) {super (millisInFuture, countDownInterval) ;}@ Override public void onFinish () {TV. setText ("finish") ;}@ Override public void onTick (long millisUntilFinished) {TV. setText ("Please wait 30 seconds (" + millisUntilFinished/1000 + ")... "); Toast. makeText (MainActivity. this, millisUntilFinished/1000 + "", Toast. LENGTH_LONG ). show (); // toast has a display time delay.} the copy code mainly involves rewriting onTick and onF. Insh: the code in onFinish () is what you want to do when the timer ends. The code in onTick (Long m) is what you want to do when the countdown starts, the m parameter is the time until completion. In the constructor MyCount (), the former is the number of Countdown times, and the latter is the interval between the second, all are in milliseconds. For example, if the countdown is 30 seconds and the interval between two seconds is 1 second, two parameters can be written as MyCount ). It encapsulates background thread creation and Handler queue into a convenient class call.