Android AlarmManager 服务不需唤醒
我正在编写一个 Android 服务,每天下午 5 点检查一个网站。如果满足某些条件,它将发出通知,如果不满足,则不会发生任何事情。
起初,我尝试将其实现为后台服务,该服务将启动一个线程,该线程会休眠一定毫秒数,直到下一个下午 5 点,然后进行检查并在需要时发出通知,然后再次返回休眠状态。 如果我在服务启动时接近下午 5 点,则此方法有效,但否则无效,并且我在谷歌搜索后了解到 Android 可以随时终止服务,所以我想这不是推荐的解决方案。
我找到的建议似乎都指向 AlarmManager,这也是我在实现上述解决方案之前首先看到的,但它有一个缺点。如果我使用 RTC 作为类型,那么直到用户开始使用手机时才会执行检查,我希望即使空闲也能完成检查。但是,我不想使用 RTC_WAKEUP,因为除非我断定应该引发通知,否则我不想打开屏幕。
我想这应该是可行的,因为这正是任何电子邮件应用程序所做的。如果电子邮件服务检查服务器是否有新邮件的频率是这样的话,则该屏幕不会每分钟出现一次。
RTC_WAKEUP真的亮屏了吗?如果是,是否有另一种首选技术来实现上述场景?
I am writing an Android service that checks a website every day at 5 PM. If certain conditions are met it will raise a Notification, if not then nothing should happen.
At first I tried to implement it as a background service that would start a thread that sleeps for the number of milliseconds until the next 5 PM, then do the check and raise the notification if needed, and then go back to sleep again.
This worked if I was close to 5 PM when the service was started, but not otherwise and I have learnt after googling that Android can kill a service at any time, so I guess this is not the recommended solution.
The recommendations I find seems to all point to the AlarmManager, which was also what I first looked at before implementing the above solution, but there is one drawback with it. If I use RTC as type, then the check won't be performed until the user starts to use the phone, I want it to be done even if idle. However, I don't want to use RTC_WAKEUP because I don't want the screen to turn on unless I conclude that the notification should be raised.
I guess it should be doable as it is exactly what any email-application does. The screen doesn't come on every minute if that is how often the email service checks the server for new messages.
Does RTC_WAKEUP really turn on the screen? If yes, is there another preferred technique for implementing the above scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RTC_WAKEUP
不打开屏幕。不,在这里,它只是“唤醒”CPU。
RTC_WAKEUP
does not turn on the screen.No. Here, it only "wakes up" the CPU.