服务与START_NOT_STICKY重新启动的 [英] Service Restarted with START_NOT_STICKY
问题描述
我有在后台运行的服务。它是好的,如果系统杀死在低内存条件下,这种服务,但是,我不希望服务由系统重新启动。
I have a service that runs in the background. It is fine if the system kills this service on low memory conditions, however, I do not want the service to be restarted by the system.
因此,为了做到这一点,我从我的onStartCommand返回START_NOT_STICKY标志这样:
So in order to do this I return the START_NOT_STICKY flag from my onStartCommand as such:
public int onStartCommand(Intent intent, int flags, int startId) {
// do stuff here
return START_NOT_STICKY;
}
然而,当我打开了一堆故意应用程序创建内存不足的情况我认为这在日志中:
However, when I open a bunch of applications on purpose to create low memory conditions I see this in the logs:
Process com.myapp (pid 3960) has died.
Scheduling restart of crashed service com.myapp/.MyService in 5000ms
Low Memory: No more background processes.
...
Start proc com.myapp for service com.myapp/.MyService: pid=4905 uid=10031 gids={3003, 1015}
所以,我的过程正在重新启动时,它不应该是。为什么是这样?根据文档START_NOT_STICKY不应该允许重新启动该服务。是否有任何其他方式prevent服务的重新启动?或者,我可以告诉大家,我的服务已重新启动?
So my process IS being restarted when it shouldnt be. Why is this? According to the documentation START_NOT_STICKY should not allow the service to be restarted. Is there any other way to prevent the restart of the service? Or can I tell when my service has been restarted?
感谢
推荐答案
有可能是多种原因重新启动服务,即使它是NOT_STICKY:
There can be multiple reasons for restarting a service even though it is NOT_STICKY:
- 如果您的服务绑定,通过使用 bindService 与BIND_AUTO_CREATE
- 如果您对服务挂起意图
- If your service is bound, by using bindService with BIND_AUTO_CREATE
- If you have pending intents on the Service
要解决这个问题,最好的办法就是解除绑定服务。
The best way to resolve this issue is by unbinding the service.
这篇关于服务与START_NOT_STICKY重新启动的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!