我有一个 android 服务负责更新时钟并在不同时间执行特定任务(如播放歌曲)。
我尝试使用 Timer.scheduleAtFixedRate(new MyTask(), 0, 1000L) 之类的东西,但我在 1 小时内延迟了 4 分钟以上。
现在我决定:
1) 使用 System.currentTimeMillis() 在 var long expectedtNextSystemMilli
中记录开始时间
2) 向此 var 添加 1 秒(1000 毫秒)以获得下一个日期/时间以再次执行任务
3) 在可运行任务内部计算“实际系统时间”和来自 var 的预期时间之间的延迟,以安排任务的下一次执行。
经过几次测试,我注意到还有一个延迟问题,并在文档中读到不鼓励使用 System.currentTimeMillis() 来测量耗时,我们必须使用 SystemClock.elapsedRealtime()。 我也这样做了,下面是 MyTask 的代码
private class MyTask extends TimerTask {
@Override
public void run() {
Log.i(TAG, "Timer doing work.");
try {
// compute the exact next expected system time when MyTask must be executed
expectedtNextSystemMilli += 1000L;
long delay = expectedtNextSystemMilli - SystemClock.elapsedRealtime() ;
// countdown update
counter = counter - 1;
// !!! sometimes android system is slow and more than 1s is spend before this method is called
// also we have to test and loop to catch up the delay
while ( delay < 0){
Log.e("TimerTick", "ERROR time spent by system in milli = " + delay + "> 1s ");
expectedtNextSystemMilli += 1000L;
delay = expectedtNextSystemMilli - SystemClock.elapsedRealtime() ;
counter = counter - 1;
}
// test if countdown is finished
if ( counter <=0 ){
mTimer.cancel();
onFinish();
}else{
// send message to listeners
onTick(counter);
// update the delay with the new system time (in case the call to onTick has been long)
delay = expectedtNextSystemMilli - SystemClock.elapsedRealtime() ;
// schedule the next execution of this task
mTimer.schedule(new MyTask(), delay);
Log.d("TimerTick", "new delay in milli is " + delay+", elapsedRealtime = "+ SystemClock.elapsedRealtime() );
}
} catch (Throwable t) { //you should always ultimately catch all exceptions in timer tasks.
Log.e("TimerTick", "Timer Tick Failed. Raison: "+ t.getMessage());
}
}
}
跟踪这段代码,我惊讶地发现 SystemClock.elapsedRealtime() 返回了 28 秒的跳跃,如您所见
07-13 22:15:00.041: V/TimerManagerService(19377): onTick delay = 361 end , trace = 539
07-13 22:15:00.041: D/TimerTick(19377): new delay in milli is 999, elapsedRealtime = 84800078
07-13 22:15:01.041: I/TimerManagerService(19377): Timer doing work.
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -28585> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -27585> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -26586> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -25586> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -23587> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -22587> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -21588> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -20588> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -19589> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -18589> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -17589> 1s
07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -16590> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -15590> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -14591> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -13591> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -12591> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -11592> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -10592> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -9592> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -8593> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -7594> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -6594> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -5595> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -4596> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -3596> 1s 07-13 22:15:01.041: E/TimerTick(19377): ERROR time spent by system in milli = -24586> 1s 07-13 22:15:01.051: E/TimerTick(19377): ERROR time spent by system in milli = -2597> 1s 07-13 22:15:01.051: E/TimerTick(19377): ERROR time spent by system in milli = -1598> 1s 07-13 22:15:01.051: E/TimerTick(19377): ERROR time spent by system in milli = -598> 1s 07-13 22:15:01.051: V/TimerManagerService(19377): onTick delay = 331 end , trace = 540
07-13 22:15:01.051: D/TimerTick(19377): new delay in milli is 400, elapsedRealtime = 84830677
07-13 22:15:01.450: I/TimerManagerService(19377): Timer doing work.
1) log at: 07-13 22:15:00.041: D/TimerTick(19377): 以毫为单位的新延迟为 999,elapsedRealtime = 84800078
就在 schedule 之后的 run() 中 schedule => mTimer.schedule(new MyTask(), delay);
2) schedule 调用相同的 run() 但似乎花费了超过 28 秒来完成
07-13 22:15:01.041: E/TimerTick(19377): 系统花费的错误时间以毫 = -28585 (ms)> 1s
尽管 eclipse 报告只有一秒 => 两次调用之间的 22:15:00.041 22:15:01.041
另外我想我必须使用什么才能在 android 上获得准确的运行时间?
最佳答案
经过深入调查 :) 似乎没有真正的“好”解决方案,但有几个“最佳”解决方案取决于目标。
在我的例子中,我需要显示链接倒计时(秒精度)并在每个任务结束时执行播放歌曲的任务。
当我使用 SystemClock.elapsedRealtime() 时,1 小时后经常出现几分钟的延迟。
例子:
6:00:0.0 开始倒计时 1 小时(查看智能手机上的日期)
倒计时结束于 7:04:11.0 而不是 7:00:00.0
我还必须使用 Calendar .getTimeInMillis() 返回由 android 操作系统完成的“官方”日期/时间。
我想这个日期/时间经常使用不同的技术重新同步,所以在我的情况下更准确。
希望这可以帮助...
关于android - 我们可以用什么来代替 SystemClock.elapsedRealtime() 以获得可靠的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31401031/