Search code examples
androidservicebroadcastreceiver

Restarting service when app is killed in 7.0 and above


Till Android 6.0 whenever I Kill my app my service gets restarted automatically, but when I tested it in 7.1.1 my service does not gets restart it stops.

I tried restarting the service in activity onDestroy like this,

@Override
    protected void onDestroy() {
        super.onDestroy();
        Util.printMessage("onDestroy.....");
        Intent locationIntent = new Intent(getApplicationContext(), SupportService.class);
        startService(locationIntent);
        Util.printMessage("running...?..." + isMyServiceRunning(SupportService.class));

    }

But this solution did not work, can anyone suggest what is the issue or how to start the service again and same for the boot change receiver my broadcast does not get call when device is rebooted.


Solution

  • Change the return type of onStartCommand() with START_STICKY. if the service destroy by system it will start automatically... for more info about START_STICKY Click