安卓CountDownTimer - 刻度之间的附加毫秒的延迟 [英] android CountDownTimer - additional milliseconds delay between ticks
问题描述
从我的观察刻度线之间的安卓CountDownTimer countDownInterval恰好是不准确,countDownInterval经常比指定的不再是几毫秒。所述countDownInterval在我的特定的应用程序是1000毫秒,只是向下计数了一定量的时间与一个第二台阶。
From my observation the android CountDownTimer countDownInterval between ticks happens to be not accurate, the countDownInterval is regularly a few milliseconds longer than specified. The countDownInterval in my specific app is 1000ms, just counting down a certain amount of time with one second steps.
由于这一长期蜱我最终有较少蜱当时就想,当在countdowntimer运行足够长的时间它搞砸了的时候显示倒计时(2第二步发生在用户界面级别时,足够的额外毫秒总结了)
Due to this prolonged ticks I end up having less ticks then wanted when the the countdowntimer runs long enough which screws up the displayed countdown of the time (a 2 second step happens on the UI level when enough additional ms have summed up)
展望CountDownTimer源似乎可以扭转它,所以它改正这种不必要的误差但我不知道是否有已经在Java / Android的世界变得更加美好CountDownTimer可用。
Looking into the source of CountDownTimer it seems possible to twist it so it corrects this unwanted inaccuracy yet I was wondering if there is already a better CountDownTimer available in the java/android world.
谢谢哥们任何指针...
Thanks buddies for any pointer ...
推荐答案
重写
正如你所说,你也注意到,在接下来的时间 onTick()
从时间计算的previous onTick()
运行,其中介绍了的每次的勾选一个微小的错误。我改变了CountDownTimer源$ C $ C调用每个 onTick()
从开始时间指定的时间间隔。
As you said, you also noticed that the next time in onTick()
is calculated from the time the previous onTick()
ran, which introduces a tiny error on every tick. I changed the CountDownTimer source code to call each onTick()
at the specified intervals from the start time.
我建立这个于CountDownTimer框架,因此,剪切和放大器;粘贴<一href="http://grep$c$c.com/file_/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/os/CountDownTimer.java/?v=source">the来源$ C $ C 到您的项目,并为类提供一个唯一的名称。 (我打电话给我MoreAccurateTimer)现在进行一些更改:
I build this upon the CountDownTimer framework, so cut & paste the source code into your project and give the class a unique name. (I called mine MoreAccurateTimer.) Now make a few changes:
-
添加一个新的类变量:
Add a new class variable:
private long mNextTime;
修改启动()
:
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;
}
更改处理程序的 handlerMessage()
:
@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);
}
}
}
这篇关于安卓CountDownTimer - 刻度之间的附加毫秒的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!