Search code examples
androidservicebackgroundgpsforeground

Turned off "Allow background activity" setting stops location updates in foreground service


I have a foreground service requesting location updates and doing stuff with them. When the app is not on screen, it still works because the service has an ongoing notification.

But there is a battery setting, at least on my samsung device, called "Allow background activity". I wasn't paying attention to it because it says 'background', and as far as I know, as long as I have a foreground service with ongoing notification, my app is regarded by android as in foreground, and never in background.

But I've noticed that, if that setting is turned off, my ongoing notification is still there, but the gps icon disappears and location updates stop coming.

Is this normal behavior or I'm doing something wrong? Shouldn't my app be regarded as in foreground instead?

In case it is normal behavior, I'm aware now that I can detect this status with isBackgroundRestricted(), and I can use that to notify the user that they need to change it. The other question is, is there a way to programmatically open the exact settings page where the user should make this change?

Thank you!


Solution

  • I had a similar question here. Short answer is I don't think there is an intent to go to the exact screen, but you can get the user there using android.provider.Settings.ACTION_APPLICATION_SETTINGS.