Android - 当某些条件满足时如何在后台运行服务如何显示通知
如何在电池管理器 Android 应用程序的后台运行服务?
当电池电量低或满足某些条件时,我将如何显示或接收通知?
例如,对于闹钟,当当前时间等于闹钟时间时,闹钟就会响起。
How can I run services in the background of a battery manager Android application?
How would I display or receive a notification when the battery is low or when certain criteria are met?
For example, for an alarm, when the current time equals the alarm time the alarm rings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有 3 个选项:
您创建一个长期运行的服务。它会产生一个线程,定期检查您提到的事情。我不建议这样做,从长远来看,这些服务往往会被操作系统杀死。另外,您必须使用唤醒锁来防止系统在手机闲置几分钟后进入空闲状态时将 CPU 置于空闲状态 - 也会停止您的服务。
查看您可以注册广播接收器的可用系统事件。如果您找到合适的接收器(我认为有一个用于指示电池电量不足的接收器),请为它们编写接收器。
使用计时服务来注册要定期触发的意图。这些意图将启动一个服务(短时间运行的服务)来完成您的工作。
You have 3 options:
You create a long running Service. It spawns off a thread that checks the things you mentioned periodically. I wouldn't advise this tho, these services tend to be killed on the long run by the os. Also, you'd have to use Wake Locks to prevent the system to put the cpu on idle when the phone goes idle after some minutes of inactivity - also stopping you service.
Check out the available system events for which you can register Broadcast Receivers. If you find suitable ones (I think there's one for indicating low battery levels), write receivers for them.
Use the timing service to register intents to be fired off on regular intervals. These intents would start a service (a short running one) that would do your thing.