Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically restart a Service when the apk crashes

Tags:

android

I want to restart my Service automatically if the application crashes. My ideas are:

1: a BroadcastReceiver when the apk crashes and automatic restart the Service

if("android.intent.action.SOMETHING".equals(intent.getAction()))
  {
     Intent serviceLauncher = new Intent(context, Service.class);
     context.startService(serviceLauncher);
  }

2: with an uncaughtException(), but i dont know to handle that

like image 682
Boe-Dev Avatar asked Dec 22 '11 14:12

Boe-Dev


2 Answers

I think you only need return START_STICKY in onStartCommand() of Service

like image 165
Sephiroth Avatar answered Nov 09 '22 20:11

Sephiroth


You can use setUncaughtExceptionHandler

When receiving the event you can start the service again,

Another option, that might work (You need to try it), is calling StartService on the OnDestroy method.

like image 21
Oren Bengigi Avatar answered Nov 09 '22 20:11

Oren Bengigi