1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by raylight, Apr 22, 2021.

  1. raylight

    raylight Lurker
    Thread Starter

    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?
     



    1. Download the Forums for Android™ app!


      Download

       
  2. raylight

    raylight Lurker
    Thread Starter

    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.
     
Loading...
Similar Threads - Problem getting location
  1. JP123
    Replies:
    4
    Views:
    298
  2. toneman1
    Replies:
    2
    Views:
    262
  3. Robert Watson
    Replies:
    0
    Views:
    336
  4. mailvishnu
    Replies:
    1
    Views:
    329
  5. kourtneys
    Replies:
    1
    Views:
    330
  6. mlgch1
    Replies:
    0
    Views:
    1,000
  7. varamilc
    Replies:
    8
    Views:
    852
  8. varamilc
    Replies:
    7
    Views:
    1,957
  9. Juraj Puchký
    Replies:
    0
    Views:
    433
  10. Ivashu
    Replies:
    0
    Views:
    408

Share This Page

Loading...