6

I'm updating an app that currently uses the ACCESS_FINE_LOCATION permission and with new API 30 requirements, will now require the ACCESS_BACKGROUND_LOCATION permission as well.

While updating the app to add this permission, I'm also trying to use the new ActivityResultLauncher callback method of requesting permissions (https://developer.android.com/training/permissions/requesting#allow-system-manage-request-code)

This seems to work well using launching single requests along these lines:

ActivityResultLauncher<String> launcher = registerForActivityResult(
    new ActivityResultContracts.RequestPermission(), this::processPermissionResult);

launcher.launch(somePermission);

However, since I need both ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION it requires some extra code to keep track of the results from multiple callbacks (since each request will generate its own callback) so that I can take appropriate action if one, both, or none of the permissions were granted.

To simplify this a bit, I was looking to use the RequestMultiplePermissions method instead, which would conveniently give me one callback with all the results in a map:

ActivityResultLauncher<String[]> launcher = registerForActivityResult(
    new ActivityResultContracts.RequestMultiplePermissions(), this::processPermissionResult);

launcher.launch(new String[] {
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.ACCESS_BACKGROUND_LOCATION
});

However I found that the moment I request both ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION in the same request, I am no longer prompted with either permissions dialog and the callback comes back with false for both the permissions.

I can, however, make individual requests for these permissions using RequestMultiplePermissions by giving it an array containing a single permission, and then calling launch multiple times using the same approach as if I was using the single RequestPermission call, and that will work.

It seems that there some conflict/interaction between the ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION permissions when it comes to RequestMultiplePermissions? I was able to take out ACCESS_BACKGROUND_LOCATION and use a different permission, like WRITE_EXTERNAL_STORAGE, and I would get two separate permission dialog prompts.

Am I misunderstanding something about the ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION permissions? Should I not be requesting them simultaneously?

Zoe
  • 27,060
  • 21
  • 118
  • 148
acee
  • 348
  • 1
  • 6
  • 16
  • 4
    This is intended behavior on android 11 - you have to have been granted foreground permission BEFORE being allowed to ask for background permission. Additionally permission request for both at once is automatically denied. See https://stackoverflow.com/questions/64388343/activitycompat-requestpermissions-for-targetsdkversion-30-is-not-working/64388514#64388514 – Pawel Mar 17 '21 at 16:58
  • @Pawel thank you! I suspected that might be the case but hadn't yet come across documentation stating this. Thanks for providing the link to the supporting Android documentation in that other answer. Looks like I'll have to keep the requests separate in the code in this case. – acee Mar 17 '21 at 17:48
  • 3
    @Pawel could you make an answer out of it so it can get accepted? It's easier for others to find the answer then. – Benjamin Mesing Oct 14 '21 at 10:52

0 Answers0