从我的观察来看,android的倒计时时间间隔恰好不准确,倒计时间隔通常比指定的时间长几毫秒。在我的特定应用程序中,倒计时间隔是1000毫秒,只需用一秒钟的步骤倒计时一段时间。
由于这种长时间的计时,当倒计时计时器运行足够长的时间(这会导致显示的倒计时时间变短)时,我得到的计时时间比预期的要少(当累计了足够多的额外ms时,在ui级别上会出现2秒的步骤)
查看CurnDealTimeToin的来源,似乎有可能扭转它,从而纠正这种不必要的错误,但我想知道是否已经有一个更好的在Java/Android世界中的CurnDebug定时器。
谢谢朋友们的指点…

最佳答案

重写
正如您所说,您还注意到,下一次inonTick()是从上一次运行的时间计算出来的,这会在每个刻度上引入一个微小的错误。我更改了倒计时程序源代码,以从开始时间开始的指定间隔调用每个onTick()
我是在倒计时框架的基础上构建的,所以将the source code剪切粘贴到您的项目中,并给类一个唯一的名称。(我给我的计时器打了个电话。)现在做一些改变:
添加新的类变量:

private long mNextTime;

更改onTick()
public synchronized final MoreAccurateTimer start() {
    if (mMillisInFuture <= 0) {
        onFinish();
        return this;
    }

    mNextTime = SystemClock.uptimeMillis();
    mStopTimeInFuture = mNextTime + mMillisInFuture;

    mNextTime += mCountdownInterval;
    mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG), mNextTime);
    return this;
}

更改处理程序的start()
@Override
public void handleMessage(Message msg) {
    synchronized (MoreAccurateTimer.this) {
        final long millisLeft = mStopTimeInFuture - SystemClock.uptimeMillis();

        if (millisLeft <= 0) {
            onFinish();
        } else {
            onTick(millisLeft);

            // Calculate next tick by adding the countdown interval from the original start time
            // If user's onTick() took too long, skip the intervals that were already missed
            long currentTime = SystemClock.uptimeMillis();
            do {
                mNextTime += mCountdownInterval;
            } while (currentTime > mNextTime);

            // Make sure this interval doesn't exceed the stop time
            if(mNextTime < mStopTimeInFuture)
                sendMessageAtTime(obtainMessage(MSG), mNextTime);
            else
                sendMessageAtTime(obtainMessage(MSG), mStopTimeInFuture);
        }
    }
}

08-18 06:29