0

I am working on an app in which I have to get location after every few minutes. For this purpose I have implemented background service so that my app will not be killed. It is working fine. But on some devices like Vivo/Oppo/Infinix etc, it is not working fine. For battery optimisation, these devices have different options. These devices killed the app/service if app is consuming battery continuously in the background. Is there any way to detect or avoid such options.enter image description here

One of Vivo device have options as I mentioned in the attached image. Some devices have such options in different ways. I am unable to detect such options.

Faizan Ahmad
  • 352
  • 4
  • 20
  • You cannot "detect" this situation. All you can do is inform your users that they may have to manually add your app to a list of apps that are allowed to run in the background. Different devices have different names for this and different mechanisms. Some call this "protected apps", etc. – David Wasser Mar 28 '22 at 09:35

1 Answers1

0

All modern versions of Android stop background services after a few minutes, unless your app is also the foreground app. You have to make a foreground service instead. Although that just makes you less likely to be killed, you still can be- there's no way to prevent it totally.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • It is a foreground app, I am running foreground service. But some devices killed app too in background. – Faizan Ahmad Mar 28 '22 at 05:48
  • You aid above you have a background service. There is a difference between the two. But yes, an app can be killed at any time its not actively on screen. That's how Android works. It's really not meant to reliably do things in the background over long periods of time, it's meant to discourage that in favor of battery life. You can set an JobScheduler or WorkManager task to do this, but once the screen is off and you're in Doze mode you'll be woken at most every 15 minutes or so. There's really no getting around this. – Gabe Sechan Mar 28 '22 at 06:12
  • Thanks for your reply, I will try this too. – Faizan Ahmad Mar 28 '22 at 08:57