FileDocCategorySizeDatePackage
CountDownTimer.javaAPI DocAndroid 5.1 API4537Thu Mar 12 22:22:10 GMT 2015android.os

CountDownTimer

public abstract class CountDownTimer extends Object
Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:
new CountDownTimer(30000, 1000) {

public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}

public void onFinish() {
mTextField.setText("done!");
}
}.start();
The calls to {@link #onTick(long)} are synchronized to this object so that one call to {@link #onTick(long)} won't ever occur before the previous callback is complete. This is only relevant when the implementation of {@link #onTick(long)} takes an amount of time to execute that is significant compared to the countdown interval.

Fields Summary
private final long
mMillisInFuture
Millis since epoch when alarm should stop.
private final long
mCountdownInterval
The interval in millis that the user receives callbacks
private long
mStopTimeInFuture
private boolean
mCancelled
boolean representing if the timer was cancelled
private static final int
MSG
private Handler
mHandler
Constructors Summary
public CountDownTimer(long millisInFuture, long countDownInterval)

param
millisInFuture The number of millis in the future from the call to {@link #start()} until the countdown is done and {@link #onFinish()} is called.
param
countDownInterval The interval along the way to receive {@link #onTick(long)} callbacks.


                                                    
         
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    
Methods Summary
public final synchronized voidcancel()
Cancel the countdown.

        mCancelled = true;
        mHandler.removeMessages(MSG);
    
public abstract voidonFinish()
Callback fired when the time is up.

public abstract voidonTick(long millisUntilFinished)
Callback fired on regular interval.

param
millisUntilFinished The amount of time until finished.

public final synchronized android.os.CountDownTimerstart()
Start the countdown.

        mCancelled = false;
        if (mMillisInFuture <= 0) {
            onFinish();
            return this;
        }
        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
        mHandler.sendMessage(mHandler.obtainMessage(MSG));
        return this;