How to restart a killed service automatically?
14,731
Solution 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.
Solution 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
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on June 22, 2022Comments
-
Admin about 2 years
When a service has been killed, how to restart it automatically?
sometimes without even calling
onDestroy()
-
Abhi about 8 yearsI am doing same. working fine in nexus 5. But in xiaomi service is getting killed as soon as my application is killing.
-
Abhi about 8 yearsI am doing START_STICKY. working fine in nexus 5 and restarting the service. But in xiaomi service is getting killed as soon as my application is killing.
-
Meymann about 8 yearsIn bad cases, you can start an alarm clock intent to make sure your service is a live. Beware of using it too often, as waking the CPU costs battery life. Do you have an app that does revive swipe to dismiss?