How to restart a killed service automatically?

放肆的年华 提交于 2019-11-28 00:55:45

问题


When a service has been killed, how to restart it automatically?

sometimes without even calling onDestroy()


回答1:


I inherited an IntentService, so I had to be gentle. It worked for me when I overrode onStartCommand() but

public int onStartCommand(Intent intent, int flags, int startId) {
   super.onStartCommand(intent, flags, startId);
   return START_STICKY;
}

That is, let the parent do what it should and return START_STICKY.




回答2:


Overrides the onStartCommand() and give START_STICKY or START_REDELIVER_INTENT (depends on your needs) as the return value. The system will then make sure to restart your service until you explicitly stop the service.

http://developer.android.com/reference/android/app/Service.html#START_REDELIVER_INTENT

http://developer.android.com/reference/android/app/Service.html#START_STICKY




回答3:


If you have your service killed, the system will try to restart it later. Read more.



来源:https://stackoverflow.com/questions/4666192/how-to-restart-a-killed-service-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!