Understanding permissions for background location on Android 11 and below

Abhi
5 min readOct 29, 2020

--

Getting the permission to access location in the background is more complicated now compared to the days of Android Pie. How permissions behaves on Android 9 (and below), 10 and 11(and above) for background location are all different. How the permissions show up for the end user not only depends on the Android version but also on the app’s targetSdkVersion.

Android 9 and below

On Android Pie, accessing location in the background is not treated any differently than regular location permission and being granted location permission to an app makes it eligible to access the location in the background as well. The location permission is a simple ON or OFF switch. Just 2 options. Simple.

Android 9

targetSdkVersion = 28 (or less)

For Android 10 and above, two situations arise, apps built with targetSdkVersion <= 28 and >= 29. Lets talk about 28 (or less) first. If app’s targetSdkVersion = 28 (or less) means the code is still requesting the regular location permissions.

Android 10 with targetSdkVersion <= 28

Android 10

Android 10 onwards user will be shown 3 options when requesting permission for location. If user selects “allow all the time”, then the app will be able to access location in the background.

.

.

.

.

.

.

.

.

.

.

.

.

.

Android 11

When requesting location permission on Android 11, it will show 3 options with an additional message. The message tells the user that if she wishes to allow the app access to location all the time, then enable it in settings. So the “allow all the time” option isn’t shown via pop-up dialog anymore but accessible only in the app’s location permissions settings.

targetSdkVersion = 29 (and above)

To be able to access background location if your app targets API 29 and above, you need to add this permission to the manifest

<uses-permission android:name=”android.permission.ACCESS_BACKGROUND_LOCATION” />

Android 10

Now that this is a separate permission, on requesting regular location permission, “Allow all the time” option does not even show up. This (Left) is what is shown when you request a Manifest.permission.ACCESS_FINE_LOCATION permission. But since you’ve mentioned background location permission in the manifest, “Allow all the time” option is available in the settings though -

Android 10 with targetSdkVersion = 29

For Android 10 and up, you have to explicitly request for background location. So just like you requested for Manifest.permission.ACCESS_FINE_LOCATION, you now do so for Manifest.permission.ACCESS_BACKGROUND_LOCATION. But there’s a big gotcha here.

On Android 10 and up, if an app has not been granted access to regular location permission, then background location permission dialog doesn’t even show up. In this situation, shouldShowRequestPermissionRationale() returns true. So to get access to background location you need to first get access to foreground location permission.

After a user has granted access to foreground location, this is what a user will see when requesting for background location -

This is basically saying that you’ve given the app access to it while you you are using it, do you wanna extend that and give it access to location all the time?

So thats Android 10. Moving on to Android 11 and up.

.

.

.

.

.

.

.

.

.

.

.

.

.

Android 11

The code for background location permission for targetSdkVersion ≥29 remains same, but the behaviour on Android 11 changes. If you request a regular location permission this is what shows up -

There are two differences to note here.

  1. A new option showed up -“Only this time”. You can read more about it here https://developer.android.com/guide/topics/permissions/overview#one-time.
  2. There is no “Allow all the time” option like was there for targetSdkVersion = 28 and neither is the additional message that was shown in Android 10 present anymore.

.

.

.

.

.

.

.

.

.

.

However, if the manifest has the background permission option, this is what you’ll see in the settings screen -

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Now finally, to request and access this permission on Android 11, you CANNOT do so within the app at all. If you request permission using Manifest.permission.ACCESS_BACKGROUND_LOCATION, it will not work. In this situation, shouldShowRequestPermissionRationale() returns true. So, to get a user to give you access to the background location permission, you have to direct him to the app’s permission page shown above and instruct him to select “Allow all the time” option.

I hope this writeup helps someone. You can read more about this in the official docs here —
- https://developer.android.com/training/location/permissions#request-background-location
- https://developer.android.com/training/location/background#evaluate

--

--