6

My app is targeting Android API 28.

According to the documentation, I should be requesting ACCESS_BACKGROUND_LOCATION, along with ACCESS_FINE_LOCATION when running on Android API < 29.

I have tried doing this but in onRequestPermissionResults(), ACCESS_BACKGROUND_LOCATION is never granted and returns PackageManager.PERMISSION_DENIED. I was under the impression this would be implicitly granted when requesting another foreground permission on < 29.

Alternatively, I can remove the permission from the request on API < 29 and everything seems to work as expected but I am conscious that I would be going against the documentation.

Question

Can I omit an explicit request for the ACCESS_BACKGROUND_LOCATION on Android versions < 29 and still work as expected whilst conforming to documentation in API 29 which works?

StuStirling
  • 15,601
  • 23
  • 93
  • 150
  • The `ACCESS_BACKGROUND_LOCATION` was added in API 29, so it doesn't exist in API 28 and lower. It's not super clear, but the docs are probably talking about what happens when running on Android 10 or 11. What version of Android are you testing on when it gets automatically denied? – Daniel Nugent Feb 24 '20 at 22:40
  • I am compiling with API `29` but targetting API `28`. When testing I have tested on `23` mainly as well as `29` of course. – StuStirling Feb 24 '20 at 22:44
  • Interesting. I would expect it to be denied on 23, but not on 29. – Daniel Nugent Feb 24 '20 at 23:00
  • Sorry I was not clear. You expectations are correct. It is denied on 23 but works as expected on 29. I don't know if you've checked the documentation I linked above but it clearly says to explicitly include the permission in the request – StuStirling Feb 24 '20 at 23:34
  • Ahh, that makes sense then. On API 28 and lower, it's basically a no-op, since the permission doesn't exist in the code on those versions of Android. – Daniel Nugent Feb 24 '20 at 23:41
  • @DanielNugent could you please write up an answer and I'll accept it. I believe you are correct, just the documentation is not clear and misleading – StuStirling Mar 05 '20 at 10:47

1 Answers1

7

The documentation states:

If your app targets Android 10 (API level 29) or lower, complete the following steps to request ACCESS_BACKGROUND_LOCATION:

  1. Include the ACCESS_BACKGROUND_LOCATION permission, as well as either the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission, in your app's manifest.

  2. Request the ACCESS_BACKGROUND_LOCATION permission, as well as one of the other location permissions: ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. You can request these permissions in a single operation.

This is a little ambiguous because they don't mention specifics about how to implement, but the ACCESS_BACKGROUND_LOCATION was added in API 29, so it doesn't exist in API 28 and lower.

Apparently this wasn't the case in the past, but now if you try to use it for lower API levels, you'll get a warning:

enter image description here

You can just omit the request to ACCESS_BACKGROUND_LOCATION on API 28 or lower:

private fun requestLocationPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        ActivityCompat.requestPermissions(
                this,
                arrayOf(
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_BACKGROUND_LOCATION
                ),
                MY_PERMISSIONS_REQUEST_LOCATION
        )
    } else {
        ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                MY_PERMISSIONS_REQUEST_LOCATION
        )
    }
}

Complete code available here: https://stackoverflow.com/a/40142454/4409409

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • 4
    Sorry, I don't think I agree with your answer. You do need API level checks because on lower than 29, when you request `ACCESS_BACKGROUND_LOCATION` permission, it's coming back as denied. You either need to ignore it on older versions or not even request it. – StuStirling Mar 05 '20 at 17:04
  • 1
    Got it. I didn't realize that you did need the API version checks. Updated the answer, let me know if it's correct now! – Daniel Nugent Mar 05 '20 at 17:08