Android的计时器,当设备处于休眠状态的工作原理 [英] Android timer that works when device is sleeping

查看:151
本文介绍了Android的计时器,当设备处于休眠状态的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个体育应用程序,需要跟踪的季度/半/段所经过的时间。经过的时间需要精确到秒。比赛计时钟需要继续即使用户通过$ P $明确地将器件置于休眠模式pssing电源按钮运行。

I'm writing a sports app that needs to track the elapsed time of quarter/half/period. Elapsed time needs to be accurate to the second. The game clock needs to continue to run even if the user explicitly places the device in sleep mode by pressing the power button.

我在这第一次尝试'T通过屏幕超时停止。但我很快了解到,这是有可能规避本办法由pressing电源按钮手动将设备睡觉。此外,postDelayed()的方法正经历一些时钟漂移,在run()方法用的时间明显的结果。实际数量仍然准确,但不是被对齐,例如,在第二次5界面上并且容易被用户理解的 - 涉及启动定时器漂移,致使部分可以理解的用户混淆

My first attempt at this involved using Handler.postDelayed() to trigger the clock ticks every 200ms and WindowManager.LayoutParms.FLAG_KEEP_SCREEN_ON to ensure that the "clock" wasn't stopped by a screen timeout. But I soon learned that it was possible to circumvent this approach by pressing the power button to manually put the device to sleep. In addition, the postDelayed() approach is experiencing some clock drift, apparently a result of the time spent in the run() method. The actual numbers are still accurate, but instead of being aligned, for example, on 5 second boundaries which are easily understood by users - the timers involved start to drift, resulting in some understandable user confusion.

在一些研究,我发现techiques使用服务,的Java定时器,的AlarmManager 和的 PartialWakeLock 实现定时器。由服务本身不会解决与设备进入休眠状态相关联的问题。 Java的计时器,像服务,不解决问题与设备进入休眠状态。 AlarmManager似乎是个不错的办法,但我担心,这是不是一个合适的使用AlarmManager的(报警之间即非常短的时间间隔)。使用PartialWakeLock也看起来很有希望,但它本身并没有解决时钟漂移问题,我遇到。

After a bit of research I found techiques for using services, java timers, AlarmManager, and PartialWakeLock to implement timers. Services by themselves won't solve the problem associated with the device going to sleep. Java timers, like services, don't solve the problem with the device going to sleep. AlarmManager seems like a good approach, but I'm concerned that this isn't an appropriate use of AlarmManager (i.e., very short intervals between alarms). Using PartialWakeLock also looks promising, but by itself it doesn't address the clock-drift problem I'm experiencing.

我要去尝试AlarmManager和PartialWakeLock的组合。我们的想法是,AlarmManager将有助于打击时钟漂移和PartialWakeLock,以帮助保持code简单(五指交叉)。我希望,这种做法将导致节能,code的复杂性,以及用户期望之间取得合理的平衡。任何意见是很大的AP preciated。

I'm going to try a combination of AlarmManager and PartialWakeLock. The idea is that AlarmManager will help combat clock-drift and PartialWakeLock to help keep the code simple (fingers-crossed). I'm hoping that this approach will result in a reasonable balance between power conservation, code complexity, and user expectations. Any advice is greatly appreciated.

谢谢

推荐答案

我有我原来的岗位的部分解决方案上面。它还没有解决与所述的 postDelayed()的处理期间在计算中所花费的时间相关联的时钟漂移,但它是一个进步。此外,它看似简单,总是一个好兆头。

I've got a partial solution to my original post above. It doesn't yet address the clock drift associated with the time spent in calculations during the postDelayed() processing, but it is a step forward. In addition, it's deceptively simple, always a good sign.

原来我用的 SystemClock.uptimeMillis()我一直在使用的时候 SystemClock.elapsedRealtime()的。 2之间的差异是细微的,但是重要的。

It turns out I was using SystemClock.uptimeMillis() when I should have been using SystemClock.elapsedRealtime(). The difference between the 2 is subtle, but important.

正如您所料,我的解决方案跟踪的经过时间通过积累调用之间持续时间为postDelayed() - 的即,经过时间= elapsedTime + lastClockInterval 的。如上所述,用原来实行的 uptimeMillis()的。仔细阅读的Javadoc表明的 uptimeMillis() 不包括时间花费在深度睡眠,例如,当用户presses电源按钮。但是,的 elapsedRealtime()的方法确实包括一次深度睡眠模式度过的。所有这一切都需要跟踪跨越了漫长的睡眠周期时间,以更换使用的 uptimeMillis()的用的 elapsedRealtime()的。成功!无需使用AlarmManager,PartialWakeLock,或其他任何实质上更复杂。当然,这些方法还是有用途的,但他们是矫枉过正实现简单的经过时间的时钟或定时器时。

As you might expect, my solution keeps track of elapsed time by accumulating durations between calls to postDelayed() - i.e., elapsed time = elapsedTime + lastClockInterval. As stated above, the original implementation used uptimeMillis(). Careful reading of the javadoc reveals that uptimeMillis() doesn't include time spent in "deep sleep", e.g., when the user presses the power button. But the elapsedRealtime() method does include time spent in "deep sleep" mode. All that was required to track time across deep sleep cycles was to replace the use of uptimeMillis() with elapsedRealtime(). Success! No need to use AlarmManager, PartialWakeLock, or anything else substantially more complicated. Granted, these methods still have uses, but they are overkill when implementing a simple elapsed-time clock or timer.

接下来的问题,以解决与由与 postDelayed()的处理相关的非零执行时间的时钟漂移。我希望产卵一个线程做处理将解决这一问题,使的 postDelayed()的或多或少模仿的异步调用。另一种方法是将调整的 postDelayed()的延迟时间,以考虑到所花费的时间的 postDelayed()的。我会后我的结果。

The next problem to tackle is with the clock-drift caused by the non-zero execution time associated with postDelayed() processing. I'm hoping that spawning a thread to do the processing will address this issue, allowing postDelayed() to more or less mimic an asynchronous call. Another approach would be to adjust the postDelayed() delay time to take into account the time spent in postDelayed(). I'll post my results.

在一个不相关的音符,我的调查期间我对待自己一个 CommonsWare Warescription 。虽然我没有直接使用任何的想法从这个来源对于这个问题,我认为这将是我的机器人去到的信息源在可预见的未来。我有一个O'Reilly的订阅,通过我的日常工作​​,但我已经找到了CommonsWare书是最不一样好,如果没有更好的,关于Android开发的O'Reilly的资源的信息源。而且我发现在O'Reilly Safari浏览器的资源是pretty的好。有趣的...

On an unrelated note, during my investigation I treated myself to a CommonsWare Warescription. While I didn't directly use any ideas from this source for this problem, I do think that it is going to be my Android go-to information source for the foreseeable future. I've got an O'Reilly subscription through my day job, but I've found the CommonsWare books to be as least as good, if not better, source of information about Android development as the O'Reilly resources. And I have found the O'Reilly Safari resources to be pretty good. Interesting...

干杯, 富

这篇关于Android的计时器,当设备处于休眠状态的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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