Sitemap
AppGeniuz Technologies Pvt. Ltd.

AppGeniuz Technologies Pvt. Ltd.

Android 11 location behavior changes

5 min readNov 29, 2023

Android OS 11 has been rollout and it has many lots of new features and some major behavior changes. I’ll discuss one major change which is related to location behavior change.

Android 11 changed permission dialog behavior. When you will request for location it’ll ask with three options: 1.while using the app 2. only this time 3. deny

Android 11 location permission dialog

Only this time means: When you’ll kill your app and relaunch then it’ll again ask for permission.This maintain permission state for single app launch session.

One thing to remember, In Android 11, If user press deny two times after that permission dialog will not appear when you will request for permission. This is applicable to all permissions. means: this behavior is changed for runtime permissions.

If you want android 11 OS support for location access then remember below points:
1. Does your app need location access in foreground? Your app will be considered in foreground if any of below condition exist:
a.
When your app activity is visible
b. When your app is using foreground service(this show a persistent notification to user). In this case, even user leaves your app or press home button to take app in background. Your app will get location. It is not considered as background state.

2. Does your app need location access in Background? Your app will be considered in background if above point 1 condition does not exist.

So there are two scenario to consider in mind before to support location in android 11. One is foreground and other is background state. So lets take a look one by one:

When app needs location in foreground state:
1.
Declare one of any permission in manifest, depends on your requirement:
a) android.permission.ACCESS_FINE_LOCATION
or
b) android.permission.ACCESS_COARSE_LOCATION
2. if your app has foreground service then declare “android:foregroundServiceType=”location”” entry inside service:
<service
android:name=”MyNavigationService” android:foregroundServiceType=”location”>
3. In your activity/fragment, Request for location permission:
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
YOUR_REQUEST_CODE_ANY_INTEGER
);
}

4. You will see permission dialog with two options: 1. while using the app and 2. only this time. You choose any options, You app will work same except option 2, You will have to ask again for permission and system will show again permission dialog with same options.
5. Now your app can get location data and location updates continuously except app does not go in background(if app is not using foreground service)

NOTE: App will get locations continuously till app in foreground. If your app has foreground service then in background also, Your app will continuously get location updates. read above, I have mentioned.

When app needs location in Background state:
This is special case in android 11 OS.
1. Declare in manifest
a) android.permission.ACCESS_BACKGROUND_LOCATION
b)
android.permission.ACCESS_FINE_LOCATION
or
android.permission.ACCESS_COARSE_LOCATION

2. Important point: (For below points, System will discard your request and will not show permission dialog)
a) You can not request ACCESS_BACKGROUND_LOCATION permission before foreground related permissions(access_coarse or access_fine)
b) or You can not request background_location combined with foreground permissions(access_fine and background_location)
c) You have to request both permissions one by one. First foreground related and after that background permission.

Get AppGeniuz Technologies Pvt. Ltd.’s stories in your inbox

Join Medium for free to get updates from this writer.

3. First request for foreground location permission:
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
YOUR_REQUEST_CODE_ANY_INTEGER
);
}

4. after accept either option(not deny), request for background location permission
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION),
YOUR_REQUEST_CODE_ANY_INTEGER
);
}

5. Android 11 does not provide any direct permission dialog option to accept or deny background location. User has to do it from device settings. So when we request for background location permission then app will open device permission setting page where user has to choose either option.

6. background location permission request has different ui behavior based on target sdk version. But in both cases, User has to go in device setting page to change permission options.

When app target sdk version is below 30

when app target sdk version is 30

5. handle permission result in onRequestPermissionsResult method

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
when(requestCode){
YOUR_REQUEST_CODE_ANY_INTEGER-> {
if (grantResults.size > 0
&& permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "ACCESS_FINE_LOCATION PERMISSION_GRANTED ")
//requestBackgroundLocation
}else if(grantResults.size > 0
&& permissions[0] == Manifest.permission.ACCESS_BACKGROUND_LOCATION){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.i(TAG, "ACCESS_BACKGROUND_LOCATION PERMISSION GRANTED ")
}else{
Log.i(TAG, "ACCESS_BACKGROUND_LOCATION PERMISSION not GRANTED ")
}
}
}
}
}

6. Once user accept either option from device setting page, Your app should not restrict user. Just show user a message if he has not enabled allow all the time option and continue user to using your app.

Conclusion: You have to call one by one both permission if your app needs background location.
If your app need both permission at same time and you do not want to show both dialog one by one then show a proper explanation dialog and on click of your dialog, just open setting page and leave it to user what user select. But it is not best practice but it is your decision, sometimes it does not seems good to show both dialog at same time. When you have to open setting page after foreground permissions then why not at start time. But in this case, System kill your app or restart your app. So test this while doing same.

If it is helpful then clap and share with others. Thank you :)

--

--