Android倒计时之 CountDownTimer

作者: 汉之风云 | 来源:发表于2016-03-01 15:55 被阅读12646次
    时间都去哪了.jpg

    这里给大家介绍一种android中倒计时方法, 通过一个简单的例子来使用CountDownTimer实现倒计时10秒:

    new CountDownTimer(10000, 1000) {
    
        public void onTick(long millisUntilFinished) {
            LogUtil.i(TAG, "seconds remaining: " + millisUntilFinished / 1000);
        }
    
        public void onFinish() {
            LogUtil.i(TAG, "done!");
        }
    }.start();
    
    结果.png

    可以看出CountDownTimer每隔1秒调用一次onTick(long millisUntilFinished)方法, 倒计时结束时调用onFinish()方法.

    温馨提示:回调方法中可以直接更新UI哦~

    那么我们来简单的了解一下这个类:

    构造函数

    CountDownTimer (long millisInFuture, long countDownInterval)
    
    • 参数1: 你要倒计时的总时间, 单位ms.
    • 参数2: 你要倒计时的间隔时间, 单位ms.

    方法

    public final void cancel ()
    
    public abstract void onFinish ()
    
    public abstract void onTick (long millisUntilFinished)
    
    public final CountDownTimer start ()
    
    • 方法1cancel(): 取消当前的任务
    • 方法2onFinish(): 当前任务完成的时候回调
    • 方法3onTick(long millisUntilFinished): 当前任务每完成一次倒计时间隔时间时回调
    • 方法4start(): 开始当前的任务

    好啦~~ 对CountDownTimer的概念大家也了解的差不多了, 希望对大家的学习有所帮助~~~

    最后, 提醒大家,千万别看完就忘了哦, 要学以致用!

    相关文章

      网友评论

      • c60872c4f65a:I lost the last tick, why? many people encounter same problem. https://stackoverflow.com/questions/8857590/android-countdowntimer-skips-last-ontick

        How did yout print the log?

        D/BDNPreRollADItem__: tick-----------7
        D/BDNPreRollADItem__: tick-----------6
        D/BDNPreRollADItem__: tick-----------5
        D/BDNPreRollADItem__: tick-----------4
        D/BDNPreRollADItem__: tick-----------3
        D/BDNPreRollADItem__: tick-----------2
        D/BDNPreRollADItem__: tick-----------1
        D/BDNPreRollADItem__: onFinish-----------
        uana_001:There is a report in the issue, said 'At the start of every tick, before onTick() is called, the remaining time until the end of the countdown is calculated. If this time is smaller than the countdown time interval, onTick is not called anymore. Instead only the next tick (where the onFinish() method will be called) is scheduled.

        Given the fact that hardware clocks are not always super precise, that there may be other processes in the background that delay the thread running CountDownTimer plus that Android itself will probably create a small delay when calling the message handler of CountDownTimer it is more than likely that the call for the last tick before the end of the count down will be at least one millisecond late and therefore onTick() will not be called.

        For my application I solved this problem simply by making the tick intervals "slightly" smaller (500 ms)'

        So meybe android fixed it or a smaller delay in LZ's machine.
        刀放下好好说话::sweat: Are you kidding me?Your english is good good good
      • d0a001d22833:又涨姿势了~~~
      • hds2007:get!
        汉之风云: @hds2007 😊
      • 30f5638d6a06:谢谢分享
        汉之风云: @哈罗沃德 加油~
      • 单身狗的清香:学习了 谢谢
        汉之风云: @单身狗的清香 加油

      本文标题:Android倒计时之 CountDownTimer

      本文链接:https://www.haomeiwen.com/subject/xozgkttx.html