How to restart a killed service automatically?

When the service was killed, how to automatically restart it?

sometimes even without calling onDestroy()

+7
source share
3 answers

I inherited IntentService, so I had to be gentle. This worked for me when I redefined 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 they should and return START_STICKY.

+6
source

Overrides onStartCommand() and returns START_STICKY or START_REDELIVER_INTENT (depending on your needs) as the return value. After that, the system will 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
source

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

0
source

All Articles