Android system killed my service when I clear all recent app

Android system can stop your service anytime when they want to stop to maintain the device performance or power consumption. There are many situation where android system can stop your service like low battery, app is not in active state for a long time, device is in sleeping mode, power saving mode etc.

I developed a trick or hack(you can say that) by using that no one can stop your service (Android System, Third Party apps, User).

Note: By using this your service will never stop and may drain your battery also.

Follow the steps below :-

1) Return START_STICKY in onStartCommand.

2) Then modify the onDestroy() and onTaskRemoved() method in your service as below:

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(getApplicationContext(), "Service Task destroyed", Toast.LENGTH_LONG).show();


    Intent myIntent = new Intent(getApplicationContext(), YourService.class);

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);

    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();

    calendar.setTimeInMillis(System.currentTimeMillis());

    calendar.add(Calendar.SECOND, 10);

    alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

  Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show();

}




@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);


        Intent myIntent = new Intent(getApplicationContext(), YourService.class);

        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);

        AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.add(Calendar.SECOND, 10);

        alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

        Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show();


    }




@Override

    public int onStartCommand(Intent intent, int flags, int startId) {
     Toast.makeText(getApplicationContext(), "Service Starts", Toast.LENGTH_SHORT).show();
        return START_STICKY;
    }

Here i am setting an alarm every time whenever the service is stop (by android system or by user mannually) and using PendindIntent restart the service within 10 seconds every time.


A long running service needs the following to make it less likely to be terminated:

  1. Return START_STICKY from onStartCommand(). With this, system will re-start the service even if it has to be stopped for resources limitations.

  2. When the service is not bound to any UI component (e.g an Activity), The service must go in foreground mode by showing a foreground notification. Foreground services are less likely to be terminated by system.

  3. Set the attribute "stopWithTask"=false in corresponding <service> tag of manifest file.

Also, note that devices from some manufacturers will terminate services even with above properties due to customization:

  1. Custom task managers.
  2. Battery saving features that can prohibit non-system services.

Some application services do need to stay in background and have to be aggressively kept alive. Though this method is not recommended, but following steps can be done:

  1. Add triggers for service start : Such as Boot Complete, and Network Connected broadcasts.

  2. If service receives intents/broadcasts FLAG_RECEIVER_FOREGROUND in the intent.

  3. An extreme resort is to manually schedule a pending intent for service restart, with AlarmManager, when onTaskRemoved() is called.

Also, see:

  1. START_STICKY does not work on Android KitKat

you can use START_REDELIVER_INTENT instead of START_STICKY

The simplest explanation of these could be,

START_STICKY- tells the system to create a fresh copy of the service, when sufficient memory is available, after it recovers from low memory. Here you will lose the results that might have computed before.

START_NOT_STICKY- tells the system not to bother to restart the service, even when it has sufficient memory.

START_REDELIVER_INTENT- tells the system to restart the service after the crash and also redeliver the intents that were present at the time of crash.

I hope this will help you. thanks