保持 Android 中的服务处于活动状态
我创建了一个 Android 服务,它基本上执行以下操作:
schedule a task, using the ScheduledThreadPoolExecutor.schedule() method
when the schedule time is reached, the task is executed and a new schedule is started
因此,该服务持续有一个计时器(任务执行期间除外);有时有 2 个(独立的)计时器,具体取决于任务的结果。该服务应该一直运行并不时执行不同的任务。
一切似乎都工作正常(至少在使用 DDMS 运行时),但一段时间后(大约一个小时没有通过 DDMS 连接设备)计时器任务不再执行。让它再次工作的唯一方法是停止并重新启动服务,或者通过 DDMS 连接设备(使用 Eclipse);仅唤醒设备不会触发此操作。
看起来,Android 将服务设置为一种睡眠模式(当查看设备上的活动服务时,服务仍在运行)。
我的问题是:如何防止这种行为并保持服务始终正常运行?
我做了一些研究,发现理论上可以通过在服务中获取 (PARTIAL_WAKE_LOCK) WakeLock 来提供解决方案,但据我所知,使用此解决方案,我必须在 onStartService() 方法中获取锁,并且在 onDestroy() 处释放它,这意味着我在服务的生命周期内保持锁定(永远);我怀疑使用此方法会耗尽电池电量。
还有另一种方法可以完成同样的任务吗?
I have created an Android service which basically does the following:
schedule a task, using the ScheduledThreadPoolExecutor.schedule() method
when the schedule time is reached, the task is executed and a new schedule is started
Thus, the service continuously has a timer in place (except during execution of the task); sometimes 2 (independent) timers are in place, depending on the result of the task. The service should be running all the time and executes different tasks from time to time.
All seems to work ok (at least when running with DDMS), but after a while (something like an hour without connecting the device via DDMS) the timer tasks are not executed anymore. The only way to get it working again is either stopping and starting the service again or by connecting the device with DDMS (using Eclipse); waking up the device only does not trigger this.
It looks like, Android sets the service in a kind of sleep mode (service is still running when looking at the Active Services on the device).
My question is: how do I prevent this behavior and keep the service working all the time?
I did some research and I found something which theoretically could give a solution, by acquiring a (PARTIAL_WAKE_LOCK) WakeLock in the service, but as far as I understand, using this solution, I have to acquire the lock at the onStartService() method and release it at onDestroy(), meaning that I keep the lock during the lifetime of the service (which is forever); I suspect a battery drainage using this method.
Is there another way to accomplish the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经解决了问题,感谢楼上的回复。
我从 ScheduledPoolThreaExecutor() 切换到 AlarmManager。现在与 RTC_WAKEUP 类型一起处理所有触发器。
我没有使用 IntentService,因为我的服务必须根据警报并行执行操作(并且 IntentService 实现了排队解决方案)。原来的服务在最初的实现中一直保持活动状态,现在只有在触发警报时才会创建。
我也有这样的印象(但这只是一种直觉,而不是基于真实的测量),这种新的实现(服务在需要时创建并且不是一直处于活动状态(等待计时器事件))甚至更好设备的电池寿命。
I have solved the problem, thanks to the replies above.
I switched from the ScheduledPoolThreaExecutor() to the AlarmManager. Together with the RTC_WAKEUP type all triggers are handled now.
I did not use the IntentService, because my service has to do things in parallel based on alarms (and IntentService implements a queued solution). The original service, which was staying alive all the time in the original implementation, is now only created whenever an alarm is triggered.
I also have the impression (but that is only a gut feeling, not based on real measurements, that this new implementation (where the service is created when needed and is not alive all the time (waiting on timer evens), is even better for battery life of the device.
您需要多久执行一次,一直运行是什么意思?我猜你的意思并不是一直在执行?
我有一个服务可以做到这一点,而且效果很好:
它通过警报管理器安排警报。触发警报时获取唤醒锁,执行一些工作,然后在释放唤醒锁之前安排新的警报。
How often to you need to execute, and what do you mean by running all the time? I guess that you don't mean be executing all the time?
I have a service that does this, and it works quite well:
It schedules an alarm with the alarm manager. Acquires a wakelock when the alarm is triggered, performs some work, and then schedules a new alarm before releasing the wake lock.
使用 IntentService 进行服务实现,并向 AlarmManager 注册挂起的意图,以便根据您需要的时间触发这些意图。
Use IntentService for your service implementation and register pending intents with AlarmManager to trigger those intents on the time basis you need.
我之前在应用程序中使用过唤醒锁,并且不需要在 onDestroy() 中释放它们,
我确实有以下内容,并且效果很好:
不确定它是否会有多大帮助,但如果我不发布任何内容,它肯定不会有帮助:)
I have used wake locks in an app before and I did not need to release them in the onDestroy(),
I literally had the following and it worked perfectly:
Not sure if it will help much but it definitely wont help if I don't post anything :)