当前位置:   article > 正文

CountDownTimer使用

countdowntimer

CountDownTimer

在做项目的时候,如果涉及到倒计时的功能的时候,都会使用到这个类,但是,常在河边走,哪有不湿身的。虽然经常使用,但是还是掉到了一次坑里面。

掉坑经历

背景

智能家居有这样一个需求,在设备成功配网以后,第一次初始化设备的shadow时比较耗时,所以要求15秒内,每3秒请求一次,有一次成功,则完成设备的初始化。

解决方案

15秒内,所以使用到了countDownTimer做倒计时;
每3秒请求一次,则使用Timer和TimerTask;
timerTask每三秒执行一次请求shadow状态的Topic,有一次成功获取到shadow的正确状态,则关闭countDownTimer、timer、timerTask;

掉坑

关闭countDownTimer,调用了cancel();
关闭timer,调用了cancel();
关闭timerTask,调用了cancel();
坑就在这系列cancel(),countDownTimer的onFinish()回调里关闭timer和timerTask。

弃坑

背景

15秒内成功请求到了shadow的正确状态,但是timerTask还在一直执行,什么鬼,不是全部cancel()了吗?

解决方案

然后就一路debug,突然发现,执行countDownTimer的cancel()方法后,并没有调用onFinish(),纳尼,这是什么情况?
进入CountDownTimer源码:

public abstract class CountDownTimer {

    /**
     * Millis since epoch when alarm should stop.
     * 倒计时的总时长
     */
    private final long mMillisInFuture;

    /**
     * The interval in millis that the user receives callbacks
     * 收到回调的间隔时间
     */
    private final long mCountdownInterval;

    private long mStopTimeInFuture;
    
    /**
    * boolean representing if the timer was cancelled
    */
    private boolean mCancelled = false;

    /**
     * @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) {
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    }

    /**
     * Cancel the countdown.
     */
    public synchronized final void cancel() {
        mCancelled = true;
        mHandler.removeMessages(MSG);
    }

    /**
     * Start the countdown.
     */
    public synchronized final CountDownTimer start() {
        mCancelled = false;
        //总时长小于等于0,则调用onFinish()
        if (mMillisInFuture <= 0) {
            onFinish();
            return this;
        }
        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
        mHandler.sendMessage(mHandler.obtainMessage(MSG));
        return this;
    }


    /**
     * Callback fired on regular interval.
     * @param millisUntilFinished The amount of time until finished.
     */
    public abstract void onTick(long millisUntilFinished);

    /**
     * Callback fired when the time is up.
     */
    public abstract void onFinish();


    private static final int MSG = 1;


    // handles counting down
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            synchronized (CountDownTimer.this) {
                if (mCancelled) {
                    return;
                }

                final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();

                if (millisLeft <= 0) {
                    onFinish();
                } else {
                    long lastTickStart = SystemClock.elapsedRealtime();
                    onTick(millisLeft);

                    // take into account user's onTick taking time to execute
                    long lastTickDuration = SystemClock.elapsedRealtime() - lastTickStart;
                    long delay;

                    if (millisLeft < mCountdownInterval) {
                        // just delay until done
                        delay = millisLeft - lastTickDuration;

                        // special case: user's onTick took more than interval to
                        // complete, trigger onFinish without delay
                        if (delay < 0) delay = 0;
                    } else {
                        delay = mCountdownInterval - lastTickDuration;

                        // special case: user's onTick took more than interval to
                        // complete, skip to next interval
                        while (delay < 0) delay += mCountdownInterval;
                    }

                    sendMessageDelayed(obtainMessage(MSG), delay);
                }
            }
        }
    };
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116

cancel()方法

 /**
     * Cancel the countdown.
     */
    public synchronized final void cancel() {
        mCancelled = true;
        mHandler.removeMessages(MSG);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

发现并没有调用onFinish()方法,只是发送了移除message的信息。所以,调用cancel()方法并不会调用onFinish()方法,只是调用handler的removeMessages()方法。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/808947
推荐阅读
相关标签
  

闽ICP备14008679号