-1

The service gets killed after removing the app from recent apps. But this should not be killed and run always on the background. I see that the service is running when the app is open. It's still running when I minimize the app via home-button. But it will stop if I kill it as mentioned above. How do I solve this?

public class NotificationService extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }



    @Override
    public void onCreate() {
        Toast.makeText(getApplicationContext(),"service started",Toast.LENGTH_LONG).show();



    }

    @Override
    public void onDestroy() {
        Toast.makeText(getApplicationContext(),"service stopped",Toast.LENGTH_LONG).show();

    }

    @Override
    public int onStartCommand(final Intent intent,
                              final int flags,
                              final int startId) {
        onTaskRemoved(intent);
        //code
        return  START_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {


        Intent myIntent = new Intent(getApplicationContext(), NotificationService.class);
        myIntent.setPackage(getPackageName());
        startService(myIntent);
        super.onTaskRemoved(rootIntent);


    }


}

I don't want any notification for service as in case of foreground service.

  • Does this answer your question? [Android system killed my service when I clear all recent app](https://stackoverflow.com/questions/36324794/android-system-killed-my-service-when-i-clear-all-recent-app) – Pawel Nov 11 '19 at 21:32
  • No, I have gone through it but the problem is same and this is for foreground service with notification and I don't want any notification – Ankur Saini Nov 11 '19 at 21:38
  • background services are only allowed to run when app is (or recently was) in the foreground. Since android Oreo it's not possible to run permanent background services. – Pawel Nov 11 '19 at 21:48
  • "A foreground service performs some operation that is noticeable to the user. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app." says doc. You cant create a foreground service without notification unless user cancel it permanently. – Eren Tüfekçi Nov 11 '19 at 22:03
  • The **autostart** for the app was disabled. So the service once killed, it was not able to restart. – Ankur Saini Nov 12 '19 at 09:44

2 Answers2

0

I got the answer for it the autostart for the app was off so that the service once killed was not able to restart. Thank you all for your time!

0

Direct quote from a blog that Google published:

In its continuous effort to improve user experience, the Android platform has introduced strict limitations on background services starting in API level 26. Basically, unless your app is running in the foreground, the system will stop all of your app's background services within minutes.

You should consider using WorkManager. There is a lot of resources on how to use it, including samples, code-labs and blogs.

Isai Damier
  • 976
  • 6
  • 8