• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Problem getting location inside a foreground service on Android 11 when screen is off

raylight

Lurker
I have an app that gets location updates on a foreground service, even when the screen is off. On Android 10 downwards it works perfectly fine, I've created a foreground service declaring the permissions on the Manifest file and it has foregroundServiceType="location". For testing purposes, I've created a POST request where I send the location updates to my computer so I can see when the location is being updated even when the screen is off.

So far everything is fine. However, on Android 11, I don't receive location updates when the screen is off. I've already read resources like this question on Location Updates on Android 11 from StackOverflow telling me that besides getting the ACCESS_FINE_LOCATION, I also need to get the ACCESS_BACKGROUND_LOCATION separately, just so I can get location updates. But that's not what I'm seeing in practice, even though when I ask for ACCESS_BACKGROUND_LOCATION in a different moment as dangerous permission. I only get location updates while my screen is on (it doesn't matter if my app is opened or not). The moment I turn off my screen, I don't receive any updates on my location. Am I missing any extra detail here? Is there anything else I should be doing for receiving location updates when the screen is off on Android 11?
 
It turned out that my "powersaving" mode was enabled. On my tests, I've realized that this only happens if we use the sdk 29 or above it. On sdk 28 this problem didn't exist as I could capture location with the screen off even if the powersaving setting was enabled (as long as the battery optimization was off).

So, just turning off the powersaving setting solved the issue for me. It wasn't an issue with Android 11, Android 10 working with sdk 29 does the same.
 
Back
Top Bottom