android 屏幕关闭时自动启动服务
我正在开发一个运行服务的应用程序。该服务会在系统启动或应用程序启动时自动启动。我的经理要求我定期检查它是否仍在运行(例如每 5 分钟)或每次屏幕关闭时检查它是否仍在运行,如果没有运行则再次启动该服务。
我发现的最接近的解决方案是这样的: http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
仅当应用程序仍在运行时才会重新启动服务,如果用户停止它,则不太可能或者用一些任务管理器杀死它。
除了启动或活动注册接收器之外,是否可以重新启动服务? (作为用户考虑,我真的不希望应用程序一直运行,无论我是否愿意。)
I am working on an application that runs a service. This service starts automatically when the system boots or the app is started. My manager asked me to check if it is still running periodically (eg. every 5 minutes) or each time the screen is turned off and then start the service again if not running.
The closest solution I found was this:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
This restarts the service only if the application is still running, which is unlikely if the user stopped it or killed it with some task manager.
Is it possible to have the service restarted other than boot-up or activity registered receiver? (Thinking about it as a User, I would not really like to have an application running all the time weather I wanted it or not.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么服务上需要看门狗?如果是为了阻止Android回收它来回收用户资源,请考虑使用 startForeground() 方法如本问题所述:这要求您的服务有一个通知图标,向用户提供指示。倾听你自己的反对意见:避免对用户偷偷摸摸!
Why do you need a watchdog on the service? If it's to prevent Android from recycling it to reclaim user resources, consider using the startForeground() method as discussed in this question: this requires you to have a notification icon for your service, providing an indication to the user. Listen to your own objection: avoid sneakiness towards the users!