SystemClock.elapsedRealtime()漂移时,该设备处于低功率模式 [英] SystemClock.elapsedRealtime() drift when the device is in low-power mode

查看:230
本文介绍了SystemClock.elapsedRealtime()漂移时,该设备处于低功率模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据API文档,以及previous <一个href=\"http://stackoverflow.com/questions/4918192/android-timer-that-works-when-device-is-sleeping\">SO线程中, SystemClock.elapsedRealtime()应该保持即使设备处于睡眠状态精确的时间。这不是我所观察到。

According to the API docs, as well as previous SO threads, SystemClock.elapsedRealtime() is supposed to keep accurate time even when the device is sleeping. This is not what I observe.

我写了一个简单的时钟它坐落在一个,而(真)循环和更新基于 SystemClock的价值在屏幕上的时间。 elapsedRealtime()。如果我在两个装置,例如同步时钟通过NTP,然后进行切换屏幕打开和关闭的设备之一几次,显示的时间会漂移达±0.7秒。 (当手机没有连接到外接电源,因此睡眠模式是可能的罪魁祸首这里这只是发生)。

I have written a simple clock which sits in a while (true) loop and updates the time on screen based on the value of SystemClock.elapsedRealtime(). If I synchronize the clocks on two devices, e.g. via NTP, and then proceed to toggle the screen on and off a few times on one of the devices, the displayed time will drift by up to +/- 0.7 seconds. (This only happens when the phone isn't connected to external power, so sleep mode is the likely culprit here).

这是正常的吗?这是Android的一个bug?有什么办法通过睡眠/唤醒循环保持约20毫秒的计时精度?

Is this normal? Is this a bug in Android? Is there any way to keep ~20 millisecond timing accuracy through the sleep/wake cycles?

推荐答案

我一直有同样的问题,所以今晚我试图从改变我的计时​​方法SystemClock.elapsedRealtime() System.currentTimeMillis的()。到现在为止还挺好。当我点击关闭应用程序,回来的时候是不是漂流。结果
我曾与一个服务了Runnable进行了测试。我目前正在与Runnable接口测试它的主要活动和使用共享preFS存储一切,当应用程序失去焦点。
我还没有决定,其中这2种方法是最好的 - 但是在的currentTimeMillis 似乎不是从漂流时间问题遭受 elapsedRealtime 一样。

I have been having the same issue, so tonight I tried changing my timing method from SystemClock.elapsedRealtime() to System.currentTimeMillis(). So far so good. The time is not drifting when I click off the app and back.
I have tested it with the Runnable in a service. I am currently testing it with the Runnable in the main activity and using sharedprefs to store everything when the app loses focus. I haven't decided which of these 2 methods is best yet - but the currentTimeMillis seems to not suffer from the drifting time issue elapsedRealtime does.

我知道,谷歌不推荐使用的currentTimeMillis 定时的目的,但似乎它工作更好地为保持正确的时间,当应用程序失去焦点。

I know google doesn't recommend using currentTimeMillis for timing purposes, but it seems like it works better for keep correct time when the app loses focus.

这篇关于SystemClock.elapsedRealtime()漂移时,该设备处于低功率模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆