CountDownTimer countdown timer, countdowntimer

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.