在指定日期重复闹钟
假设我的网站每周二、周四和周六都会更新。我不知道这些更新的确切时间,所以我想每小时进行一次轮询以检查是否有新内容可用。最好的方法是什么?我能想到的只有两件事:
- 始终每 1 小时重复一次警报,检查当天是否是星期二、星期四或星期六,如果是,则从服务器进行轮询。
- 在 AlarmManager.INTERVAL_DAY 的 BOOT_COMPLETED 处设置重复警报,并将 HOUR_OF_DAY 和 MINUTE 设置为零。每次闹钟响起时检查是星期二、星期四还是星期六。如果为 true,则设置另一个间隔为 1 小时的重复闹钟。
还有其他可能更优雅的方式吗?
So let's say my site gets updated every Tuesday, Thursday and Saturday. I don't know the exact time of those updates, so I would like to do polling every hour to check if new content is available. What would be the best way to do this? Only 2 things I can think of:
- repeating the alarm every 1 hour at all times, check if the day is Tuesday, Thursday or Saturday and if it is, then do the polling from the server.
- setting a repeating alarm at BOOT_COMPLETED for AlarmManager.INTERVAL_DAY with HOUR_OF_DAY and MINUTE set to zero. every time the alarm gets fired check if it's Tuesday, Thursday or Saturday. if true, set another repeating alarm with a 1 hour interval.
Any other, possibly more elegant ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用接收器启动完成,并每次设置下一个相关日期上午 12:00 的闹钟。
使用日历获取天数。
然后在您开始的服务中每小时设置一个新闹钟,直到晚上 11:59。晚上 11:59 的闹钟将为下一个适当的日子设置闹钟。
我建议使用带有待处理意图的广播接收器来处理警报,如果时间合适,它将启动服务。
请记住,使用partial_wakelock可能会更好
Use a receiver for boot completed, and every time set an alarm for the next relevant day at 12:00am.
Use Calendar to get days.
Then in the service that you start set a new alarm for every hour, until 11:59 pm. The alarm at 11:59 pm would then set an Alarm for the next appropriate day.
I would reccomend using a BroadcastReceiver with the pending Intent for the alarms which will start a service if the time is right.
Remember, it may be good to use a partial_wakelock