0

I have seen the default behaviour of many services, that they are restarted when killed by system or user.

Is it possible that if my service is killed or crashed the android device is rebooted instead of my service itself being restarted.

If so, then have android provided some mechanism that we can use to achieve the above.

Someone
  • 35
  • 4

1 Answers1

0

First of all, it is really very bad pattern to reboot device on service destroy.

Anyways, you can achieve this using below code

public class demoService extends Service
{


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e(TAG, "onStartCommand");

}

@Override
public void onDestroy() {
    super.onDestroy();
    // this won't restart your phone instead it will ask for action
    Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
    i.putExtra("android.intent.extra.KEY_CONFIRM", true);
    startActivity(i);
}

}
Shrenik Shah
  • 1,900
  • 1
  • 11
  • 19
  • yes, it is not recommended to reboot device on a service destroy. – Someone Feb 21 '17 at 11:40
  • Also if the service crashes then it is not necessary that the ondestroy() function will get called. So is there any other means to achieve the above? – Someone Feb 21 '17 at 11:43
  • @SachinDagur check this out, may help you to resolve your second concern http://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-on-android – Shrenik Shah Feb 23 '17 at 10:17