如何区分进入Doze维护窗口和退出Doze的设备 [英] How to differentiate between the device entering a Doze maintenance window and coming out of Doze
问题描述
我有一种方法,该方法会定期运行以保持网络连接正常运行.在打ze模式下,我想禁止它定期运行,而在维护时段内仅运行一次.当设备从Doze中退出时,我希望该方法再次被定期调用.我该怎么做?
I have a method which periodically runs to keep a network connection alive. During Doze mode, I would like to disable it from running periodically and run exactly once during a maintenance window. When the device comes out of Doze, I would like the method to be called periodically again. How can I accomplish this ?
我已经注册了一个接收器,该接收器侦听 PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED
广播,然后调用 PowerManager.isDeviceIdleMode()
.但是,在两种情况下(在维护窗口期间以及设备退出打ze模式时)都返回false.我该如何区分它们?
I have registered a receiver that listens for the PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED
broadcast and then calls PowerManager.isDeviceIdleMode()
. However, this return false for both cases - during a maintenance window and when the device exits Doze mode. How can I differentiate between them ?
推荐答案
我遇到了同样的问题,尽管我认为android必须通过在接收器中发送不同的状态来区分这一点,或者提供一个函数来询问这种状态,但我确实做到了未找到任何相关内容.
i faced the same problem, although i think android must differentiate this by sending a different state in the receiver or provide a function to ask such a state, i did not found anything related.
请记住,在经过9-10分钟的时间后再次打do睡时,ACTION_DEVICE_IDLE_MODE_CHANGED再次被触发.
remember, the ACTION_DEVICE_IDLE_MODE_CHANGED is fired again when going back to doze after a period of time of 9-10 minutes.
解决方案将是:
当前设备状态->处于空闲状态"
CURRENT DEVICE STATUS -> "IN DOZE IDLE"
当您收到ACTION_DEVICE_IDLE_MODE_CHANGED并且isDeviceIdleMode()为false时您的状态是出于怀疑"-做好维护窗口的工作,并在未来15分钟内设置一个闹钟.
when you receive ACTION_DEVICE_IDLE_MODE_CHANGED and isDeviceIdleMode() is false your status is "out of doze suspicion" -DO your mantenience window work AND setup an alarm for 15 minutes in the future.
在下一个模式更改时.
如果在警报之前触发了ACTION_DEVICE_IDLE_MODE_CHANGED并且isDeviceIdleMode()为true,则它是一个维护窗口.不打false睡",(关闭闹钟).
if the ACTION_DEVICE_IDLE_MODE_CHANGED is fired before the alarm and isDeviceIdleMode() is true, so, it was a mantenience window. "out of doze false", ( turn off the alarm ).
如果首先触发警报,则表示它是已完全确认of睡"-在警报中进行无人打"设置
if the alarm is fired first, this means it was a "out of doze true confirmed" -Do your setup for "out of doze" in the alarm
打ze的时间在文档中.一定要先检查一下.(将来可能会发生变化,因为已经发生的情况*可能会有一个常数来获取该值-我不知道).
The timings of doze are in docs. be sure to check that first. ( may change in the future, as already has happened * may be there is a constant to get that value --i don't know ).
我认为这是hack,而不是完整的联合解决方案.但它会一直起作用,直到以后在Android上进行的任何扩展工作都可以提供对ACTION_DEVICE_IDLE_MODE_CHANGED的另一个调用,并且状态为闲置".
i consider this as a hack rather than a full fedged solution. but it works until any future expanded work on android provide another call to ACTION_DEVICE_IDLE_MODE_CHANGED, with "out of doze" status.
这篇关于如何区分进入Doze维护窗口和退出Doze的设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!