Background Location Changes
Android 10 introduced ACCESS_BACKGROUND_LOCATION
as a new permission. If you want your app to be able to access location data from the background, you need to hold this permission. This is a dangerous
permission, one that you will need to request at runtime in addition to having it in the manifest.
ACCESS_BACKGROUND_LOCATION
in Android 10 in the "Location Access Restrictions" chapter of Elements of Android Q!
Android 11 does not change any of that.
However, what Android 11 does change is when you can ask for it. On Android 10, you could request ACCESS_BACKGROUND_LOCATION
at the same time as you requested ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
. In Android 11, you have to request it separately and after requesting ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
.
The BackgroundLocation
sample module in the book’s sample project requests ACCESS_FINE_LOCATION
, then asks for ACCESS_BACKGROUND_LOCATION
after ACCESS_FINE_LOCATION
is granted (and the user clicks a button). This module also has two product flavors, to demonstrate two different types of builds:
Product Flavor | targetSdkVersion |
---|---|
quince |
29 |
rutabaga |
R |
(remember: Android versions are not “tasty treats” anymore!)
The following sections illustrate what you get when you request ACCESS_FINE_LOCATION
, followed by ACCESS_BACKGROUND_LOCATION
.
Android 10
We start off with a dialog offering to grant permission only while using the app:
Later, if you request ACCESS_BACKGROUND_LOCATION
, the user gets a dialog offering to upgrade your access to “all the time”:
Android 11, targetSdkVersion
29
While your targetSdkVersion
remains at 29, the first difference is that the first dialog offers the “Only this time” option, discussed earlier in this chapter:
The “upgrade” dialog that you get when you request ACCESS_BACKGROUND_LOCATION
is also a bit different. The only direct option that the user can choose is to keep their current value:
In order to actually grant you ACCESS_BACKGROUND_LOCATION
, the user has to click that “Allow in settings” link, which will bring up the permission screen for this permission group for your app in Settings:
Android 11, targetSdkVersion
30
Once you upgrade to targetSdkVersion
of 30, the initial dialog remains unchanged.
Later, when you request ACCESS_BACKGROUND_LOCATION
, the user is taken straight to the Settings app, bypassing that intermediate dialog and its “Allow in settings” link. The idea is that you would provide your own UI explaining what is about to happen, before you request the ACCESS_BACKGROUND_LOCATION
permission.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.