Android倒计时器——CountDownTimer - 封起De日子

时间:2024-02-17 10:42:27

Android倒计时器——CountDownTimer

Android倒计时器——CountDownTimer

说明

第一个参数是倒计时的时间
第二个参数是多长时间执行一次回调

/**
 * @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.
 */
public CountDownTimer(long millisInFuture, long countDownInterval)

初始化

CountDownTimer timer = new CountDownTimer(Long.MAX_VALUE, Constants.LOOP_TIME) {

        @Override
        public void onTick(long millisUntilFinished) {
            // 倒计时当中的回调
        }

        @Override
        public void onFinish() {
            // 倒计时结束后的回调
        }
    };

开始倒计时

timer.start();

停止倒计时

timer.cancel();