Search code examples
androidandroid-activityandroid-servicebackground-processandroid-service-binding

Android Service: Restarting once killed or removed from task stack from phone(Clear All) tasks


I am trying to run my service continuously in background. I understand it will drain a lot of battery but still it is a use case for me.

Case 1: Starting BackgroundService using startService(intent) method. Case 2: Starting a BoundService using bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);

In both the case, my service onStartCommand code is like below,

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            while(count < 10000){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count++;

            }

        }
    }).start();
    // TODO: flag not start itself when stopped
    // TODO: use START_STICKY to keep running
    return START_STICKY;
}

Scenario when my service does not die means it will keep running:

case 1: If I press HOME button. case 2: If I press back and come out of the application.

Scenario when my service will definitely be killed:

case 1: If I remove my application from task stack of the phone. case 2: I go to settings and stop the service.

I want my service to get started once it will be killed in either of the scenarios mentioned above.

I have referred many questions from stackoverflow to do like this,

  @Override
    public void onDestroy() {
        super.onDestroy();

        startService(new Intent(this, BackgroundService.class));
    }

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

        startService(new Intent(this, BackgroundService.class));

    }

But definitely onDestroy() is not that kind of method which will be called every time. I have checked it in my logs.


Solution

  • If the user performs a "force stop" on your app, your Service will be killed and there is nothing you can do to stop it. There is also nothing you can do to restart your Service automatically because the user has told Android that it doesn't want your app to run anymore. The user must manually restart your app.

    If you remove your app from the "list of recent tasks", Android will kill the OS process hosting your app. Assuming your Service is also running in this OS Process, it will also be killed. In this case, assuming that you have returned START_STICKY from onStartCommand(), Android will restart your Service in a new OS process.

    There are certain devices (Xiaomi, Huawei, some other Chinese phones, some LG and Lenovo devices) where your app must be manually added to a list of "protected apps" or "apps that are allowed to run in the background" in order for Android to restart your Service automatically. On these devices, Android will not automatically restart your Service even if you have returned START_STICKY from onStartCommand().

    See also Situations when a Service's onDestroy() method doesn't get called?