Implementation of the Countdown function of Android (countdowntimer)

Source: Internet
Author: User

When I went to the Forum, I saw a netizen ask a question and said the countdowntimer class. You can see from the name above and record the download time. Encapsulate background thread creation and handler queue into a convenient class call.

I checked the official documentation. This class is simple and has only four methods. It involves ontick, onfinsh, cancel, and start. The first two are abstract methods, So rewrite them.
Below is a small example officially given:

 new CountdownTimer(30000, 1000) {     public void onTick(long millisUntilFinished) {         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);     }     public void onFinish() {         mTextField.setText("done!");     }  }.start();

The code of the user who directly used it was slightly changed, a simple demo.

Package CN. demo; import android. app. activity; import android. OS. bundle; import android. content. intent; import android. OS. countdowntimer; import android. widget. textview; import android. widget. toast; public class newactivity extends activity {private mycount MC; private textview TV; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. main); TV = (textview) findviewbyid (R. id. show); MC = new mycount (30000,100 0); MC. start () ;}// end func/* 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 (newactivity. this, millisuntilfinished/1000 + "", toast. length_long ). show (); // toast display time delay }}}

The main method is to override the ontick and onfinsh methods. The code in onfinish () is what you need to do when the timer ends. The code in ontick (long m) is what you need 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.

You can use the MC. Cancel () method to cancel the task.

Related Article

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.