CountDownTimer countdown timer, countdowntimer
In the past, many countdown requirements had to be written by myself. Today, we found that android originally had a countdown class CountDownTimer, which is suitable for sending text messages and waiting for verification codes.
The code shows the 60 s countdown function in a TextView.
Public class MainActivity extends AppCompatActivity {private TextView test; @ Override protected void onCreate (Bundle savedInstanceState) {test = (TextView) findViewById (R. id. test); timer. start ();} private CountDownTimer timer = new CountDownTimer (60000,100 0) {@ Override public void onTick (long millisUntilFinished) {test. setText (millisUntilFinished/1000) + "resend after seconds");} @ Override public void onFinish () {test. setText ("Get Verification Code ");}};}
Call timer. start (); start countdown
In CountDownTimer timer = new CountDownTimer (60000,100 0), the first parameter indicates the total time, and the second parameter indicates the interval. This means that the method onTick will be called back every second, and then the onFinish method will be called back 60 seconds later.