直播商城开发 Android 倒计时出现误差解决方式

CountDownTimer的问题
发现跳秒、不出现1的问题之后,首先将每次onTick()的回调时间打印了出来(这里忘记保存log了,就口述一下)。在网上搜到的大家遇到的问题,大多数是因为,间隔时间设置为1000ms,但是回调的时候每次都要多出几毫秒或十几毫秒,而我的设备在实际测试中,多了40ms,即每倒计时25s,就会跳过一秒的显示。

首先我们要看一下CountDownTimer的源码,代码很简单,网上的分析也一抓一大把,这里就不多说了。贴出关键部分:

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);
    }
}

代码中关于delay的计算,是使用onTick执行前后的时间去计算得来的,即delay之后onTick的执行时间;但是sendMessageDelayed并不能保证精确到1毫秒,包括onTick前后的判断、读取数据,这部分的耗时都没有算在内。因机型不同、使用方式不同,造成了不同的时间延迟。

自定义倒计时器
实现方式与CountDownTimer一样,通过Handler来实现。因为业务需要,直接做成了秒倒计时,剥离了业务逻辑,将代码贴出来。

主要思路:delay的计算放到发送消息之前去计算。

计算方式:记录下开始的时间lastMills(ms),每次发送消息后,lastMills自增1000;每次发送消息前获取当前时间,计算出与期望的下一条消息的到达时间之间的差值,即本次发送消息的delay

class MyHandler extends Handler {
    private int totalSeconds;

    private long lastMills = 0L;


    MyHandler(int totalSeconds) {
        super();
        this.totalSeconds = totalSeconds;
    }

    public void start() {
        Message msg = obtainMessage();
        sendMessage(msg);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        boolean isContinue = true;
        if (totalSeconds > 0) {
            // do sth when tick
        } else {
            isContinue = false;
        }

        if (isContinue) {
            totalSeconds --;
            long internal = 1000;
            long now = System.currentTimeMillis();
            // print time
            Log.i(TAG, "handleMessage: now=" + now);
            if (lastMills != 0L) {
                internal = internal - (now - lastMills);
                lastMills += 1000;
            } else {
                lastMills = now + 1000;
            }

            Message message = obtainMessage();
            sendMessageDelayed(message, internal);
        } else {
            finish();
        }
    }

    private void finish() {
        // do things when finish
    }
}

打印出每次的时间,误差在几毫秒内波动,并不会累加;如果你的数字很整,最好在使用时增加四舍五入来消灭掉这个波动。

Johnny Deng : 08-18 13:32:47.894 handleMessage: now=1566106367894
Johnny Deng : 08-18 13:32:48.894 handleMessage: now=1566106368894
Johnny Deng : 08-18 13:32:49.894 handleMessage: now=1566106369894
Johnny Deng : 08-18 13:32:50.896 handleMessage: now=1566106370896
Johnny Deng : 08-18 13:32:51.895 handleMessage: now=1566106371895
Johnny Deng : 08-18 13:32:52.897 handleMessage: now=1566106372896
Johnny Deng : 08-18 13:32:53.896 handleMessage: now=1566106373896
Johnny Deng : 08-18 13:32:54.894 handleMessage: now=1566106374894
Johnny Deng : 08-18 13:32:55.896 handleMessage: now=1566106375896
Johnny Deng : 08-18 13:32:56.894 handleMessage: now=1566106376894
Johnny Deng : 08-18 13:32:57.895 handleMessage: now=1566106377895
Johnny Deng : 08-18 13:32:58.893 handleMessage: now=1566106378893
Johnny Deng : 08-18 13:32:59.881 handleMessage: now=1566106379881
Johnny Deng : 08-18 13:33:00.901 handleMessage: now=1566106380901
Johnny Deng : 08-18 13:33:01.893 handleMessage: now=1566106381893
Johnny Deng : 08-18 13:33:02.894 handleMessage: now=1566106382894
Johnny Deng : 08-18 13:33:03.895 handleMessage: now=1566106383895
Johnny Deng : 08-18 13:33:04.895 handleMessage: now=1566106384895
Johnny Deng : 08-18 13:33:05.895 handleMessage: now=1566106385894
Johnny Deng : 08-18 13:33:06.895 handleMessage: now=1566106386895
Johnny Deng : 08-18 13:33:07.894 handleMessage: now=1566106387894
Johnny Deng : 08-18 13:33:08.897 handleMessage: now=1566106388897
Johnny Deng : 08-18 13:33:09.897 handleMessage: now=1566106389897
Johnny Deng : 08-18 13:33:10.893 handleMessage: now=1566106390893
Johnny Deng : 08-18 13:33:11.893 handleMessage: now=1566106391893
Johnny Deng : 08-18 13:33:12.896 handleMessage: now=1566106392896
Johnny Deng : 08-18 13:33:13.895 handleMessage: now=1566106393895
Johnny Deng : 08-18 13:33:14.895 handleMessage: now=1566106394895
Johnny Deng : 08-18 13:33:15.894 handleMessage: now=1566106395894
Johnny Deng : 08-18 13:33:16.898 handleMessage: now=1566106396898
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值