Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart a killed service automatically?

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

sometimes without even calling onDestroy()

like image 640
SEed Avatar asked Jan 12 '11 07:01

SEed


2 Answers

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.

like image 149
Meymann Avatar answered Nov 03 '22 01:11

Meymann


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

like image 22
Surya Wijaya Madjid Avatar answered Nov 03 '22 00:11

Surya Wijaya Madjid