Daily Summary-Android Timer class Countdowntimer

Source: Internet
Author: User
Tags time in milliseconds

Timers are typically implemented with handler and threads or timers, but Android provides a timer class Countdowntimer. Timed execution of a countdown that stops after a period of time and is notified at a fixed interval during the countdown execution (triggering the Ontick method). The creation of a background thread and the handler queue are encapsulated as a convenient class for developers to invoke.

1     /*define an inner class for a countdown*/2     classTimecountextendsCountdowntimer {3          PublicTimecount (LongMillisinfuture,Longcountdowninterval) {4             Super(Millisinfuture, Countdowninterval);//The parameter is the total length of time, and the timing interval5         }6 7 @Override8          Public voidOnFinish () {//trigger When timing is complete9Btnresend.settext ("Resend Verification Code");TenBtnresend.setclickable (true); One         } A  - @Override -          Public voidOnTick (Longmillisuntilfinished) {//Timing Process Display theBtnresend.setclickable (false); -Btnresend.settext (millisuntilfinished/1000 + "SEC"); -         } -}

The main thing is to rewrite the two methods of OnTick and Onfinsh, the code in OnFinish () is what to do when the timer ends, and the code in OnTick (Long m) is the thing to do when you start the countdown, and the parameter m is the time until completion.

The timer creation example is as follows:

1 New Timecount (12000, 1000);
1 // Resend Verification Code 2 btnresend.setonclicklistener (new  Onclicklistener () {34    @ Override5public     void  OnClick (View v) {6         Time.start (); 7     }8 });

The two parameters in the construction method are the number of times that are inverted, which is the interval between the middle of the inverted meter and the time in milliseconds.

Daily Summary-Android Timer class Countdowntimer

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.