5

We wanted to run a service continuously in background in Android. We also know that Android has introduced background execution limits from API 26 where it clearly mentions that service will get killed within very less time once Application goes in background or swiped up and we have also observed the same when we tried it.

But currently if we see the list of Running Services window using Developer mode we see that there are few popular Apps like Instagram or Facebook where we can see that it would be running in background and would not also get killed if we swipe the application.

Even if we consider foreground services they require to show a notification but in this scenario these apps do not show any foreground service notification. So, we think that they don't use foreground services.

enter image description here

Is there any way that Android is providing these type of experience so that I could do it even for my Application?

Can anyone help me in this? Help is appreciated.

Rohan Pande
  • 301
  • 1
  • 5
  • "Is there any way that Android is providing these type of experience so that I could do it even for my Application?" -- strike a deal with the device manufacturer. Meta can do that for Facebook and Instagram. – CommonsWare Jun 15 '23 at 10:36

1 Answers1

-1

Your service is a bound service i.e., it is bound to the lifecycle of the Android component which started the service. When no clients are bound to the service, the service stops.

Use the startService() method to create an unbound service. It will keep on running even when the application is stopped.

Try making the service STICKY by using START_STICKY , this will make the system re-create your service after it is killed.

Vishnu S Dharan
  • 100
  • 1
  • 7
  • Hey @Vishnu S Dharan, I have tried this by calling startService () method only and by returning START_STICKY Flag in onStartCommand(). It will not recreate the service once it is killed when Android API is 26 and above. Please do check. onStartCommand won't be invoked again once Service is killed. – Rohan Pande Jun 15 '23 at 11:37
  • This hasn't been true in a decade. Background services have a 2 minute limit after being started if the app is not in the foreground – Gabe Sechan Jun 15 '23 at 13:57