百度翻译_timertask_Android倒计时实现思路CountDownTimer源码分析

电脑杂谈  发布时间:2016-06-08 00:04:13  来源:网络整理

你是否正在寻找关于countdowntimer的内容?让我把最高级的东西奉献给你:

countdowntimer_百度翻译_timertask

倒计时组件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) { mMillisInFuture = millisInFuture; mCountdownInterval = countDownInterval; }

二、 公有函数:
再看其 公有函数 :

  • 总共四个函数,其中两个函数是抽象函数(onTick,onFinish),需要子类实现。下面对其一一分析:
  • 2.1) start函数:
    很简单,就计算了下倒计时结束的绝对时间,同时,发送一个消息。消息如何处理,看后面分析。

    /** * Start the countdown. */ public synchronized final countdowntimer start() { if (mMillisInFuture <= 0) { onFinish(); return this; } mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture; mHandler.sendMessage(mHandler.obtainMessage(MSG)); return this; }

    2.2) cancel函数:
    取消消息。

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

    2.3) onTick函数:
    抽象函数,需要子类实现。每个间隔段触发。

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

    2.4) onFinish函数:
    抽象函数,需要子类实现。倒计时结束时回调。

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

    三、 消息处理逻辑:
    最后,阅读其消息处理逻辑,并且理解两个抽象函数回调的时间段。

    消息处理逻辑:

    // handles counting down private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { synchronized (countdowntimer.this) { final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime(); if (millisLeft <= 0) { onFinish(); } else if (millisLeft < mCountdownInterval) { // no tick, just delay until done sendMessageDelayed(obtainMessage(MSG), millisLeft); } else { long lastTickStart = SystemClock.elapsedRealtime(); onTick(millisLeft); // take into account user's onTick taking time to execute long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime(); // special case: user's onTick took more than interval to // complete, skip to next interval while (delay < 0) delay += mCountdownInterval; sendMessageDelayed(obtainMessage(MSG), delay); } } } };

    几个关键点:

  • millisLeft为倒计时剩余时间,如果倒计时剩余时间小于0,则直接调用结束方法(由子类实现);如果倒计时剩余时间小于倒计时间隔时间,则不做任何事情,直至到结束时间时发送消息,结束;若倒计时剩余时间大于倒计时间隔时间,则做一些稍复杂的处理。
  • 对于倒计时剩余时间大于时间间隔,消息处理逻辑稍复杂:回调onTick方法(由子类实现,一般就是重刷下页面时间)。这边设计比较巧妙,还考虑到了子类onTick函数处理时间,根据子类onTick函数处理时间与倒计时间隔时间大小,计算下一个消息发送时间(举例:若倒计时间隔时间为1s,当onTick处理时间为0.5s时,则下一个消息发送时间为1s-0.5s;若onTick处理时间为1.5s,则下一个消息发送时间为1s-1.5s+1s)。
  • 考虑到上述所说的onTick方法处理时间, 消息处理是同步的 。确保onTick函数是按照顺序进行回调,。
  • 以上就是关于countdowntimer的全部内容,相信你一定会非常满意。


    本文来自电脑杂谈,转载请注明本文网址:
    https://www.pc-fly.com/a/ruanjian/article-8135-1.html

      相关阅读
        发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

        热点图片
        拼命载入中...