Android:使用 Alarmmanager 暂停 Widget 更新
我刚刚完成了本教程: update-widget-in-onreceive-method (顺便说一句:您会建议对该代码进行任何改进吗?)
最后有人提到:
我只是想知道是否有办法进一步扩展此功能,以便当设备处于睡眠状态(屏幕关闭)时,更新停止。然后,当设备唤醒时,更新将恢复。
所以我的问题是:有办法做到这一点吗?如何? 或者警报管理器自动停止? - 我不这么认为。
I just went through this tutorial:
update-widget-in-onreceive-method
(btw: would you propose any improvements to that code?)
At the end someone mentions:
I'm just wondering if there is a way to extend this further so that when the device is asleep (screen off), the updates stop. Then when the device wakes up, the updates resume.
So my question: is there a way of doing this? how?
or is the alarmmanager automatically stopped? - I don't think so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以指定在调度 AlarmManager 时设备是否唤醒。
引用文档:
AlarmManager.RTC
和AlarmManager.RTC_WAKEUP
也是如此,因此您可能需要两个
AlarmManager.RTC
或AlarmManager.ELAPSED_REALTIME< 之一/代码>。这些在设备唤醒时继续,并在设备处于待机状态时停止。如果在设备睡眠时触发此警报,则当用户重新打开设备时,该警报将会发出,这正是您想要的。不,AlarmManagers 计划的警报一般不会自动取消。
这里提到的部分可以在 这部分找到教程,具体这一行:
You can specify whether the device will wake up when scheduling the AlarmManager.
Quote from the documentation:
Same goes for
AlarmManager.RTC
andAlarmManager.RTC_WAKEUP
So you probably want one of the two
AlarmManager.RTC
orAlarmManager.ELAPSED_REALTIME
. These continue while the device is awake and stop when the device is in standby. If this alarm is triggered while the device is asleep it will be delivered when the user turns the device back on though, exactly what you want. And no, the AlarmManagers scheduled alarms are not cancelled automatically in general.The part mentioned here can be found in this part of the tutorial, specific this line: