8

I have following permissions:

private static final String[] LOCATION_PERMISSIONS = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_BACKGROUND_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
};

I try to request permissions in my code with following line (activity is my current activity ref):

ActivityCompat.requestPermissions(activity, LOCATION_PERMISSIONS, 1);

Everything works fine if I have targetSdkVersion 29 or lower in my build.gradle file. Permissions dialog appear, I can press button in it to grant permissions. Everythig is fine

But! After I changed target sdk to Android 11: targetSdkVersion 30 this functionality stop working. Permission system dialog doesn't appear - and I can't grant location permissions for my app.

So can anybody help me? What am I wrong? What should I change in my code - for correctly working with Android 11?

Now with targetSdkVersion 29 I can run my app under the android 11 emulator too. May be I shouldn't increment target sdk version to: 30 at all?


The solution is:

STEP 1: The app should request foreground location permissions :

private static final String[] FOREGROUND_LOCATION_PERMISSIONS = {
            Manifest.permission.ACCESS_FINE_LOCATION,       // GPS
            Manifest.permission.ACCESS_COARSE_LOCATION,     // GPS approximate location
    };

STEP 2: The app should request background location permissions :

@RequiresApi(api = Build.VERSION_CODES.Q)
    private static final String[] BACKGROUND_LOCATION_PERMISSIONS = {
            Manifest.permission.ACCESS_BACKGROUND_LOCATION,
    };    

Only in this order!

ActivityCompat.requestPermissions(activity, FOREGROUND_LOCATION_PERMISSIONS, 1);
// Check the first statement grant needed permissions, and then run the second line:
ActivityCompat.requestPermissions(activity, BACKGROUND_LOCATION_PERMISSIONS, 1);

If I firstly trying to request background permission - it will not work.

If I request only background permissions (without already granted foreground permissions) - it will not work.

I should request foreground permissions - and then after that I should request background permission - one by one.

groff07
  • 2,363
  • 3
  • 32
  • 48
Vitaly
  • 334
  • 4
  • 14

1 Answers1

9

On android 11 requests for background location permission will be implicitly denied if user haven't granted foreground location permission first.

Requests containing both foreground and background location permission at the same time are deemed invalid:

If your app targets Android 11 (API level 30) or higher, the system enforces this best practice. If you request a foreground location permission and the background location permission at the same time, the system ignores the request and doesn't grant your app either permission.

See Request location access at runtime

Pawel
  • 15,548
  • 3
  • 36
  • 36
  • The same situation if I leave only one permission at a time: private static final String[] LOCATION_PERMISSIONS = { Manifest.permission.ACCESS_BACKGROUND_LOCATION, }; The only difference is that for target sdk 29 it's not working too. – Vitaly Oct 16 '20 at 12:42
  • 1
    @RussiaDroneFlights ACCESS_BACKGROUND_LOCATION acts different than regular permissions. From what I just tested your app needs to already hold foreground location permission or this request is immediately denied. If you do then system window will open that allows user to elevate location permission to "allow all the time". However if user doesn't do it then you won't be able to request it again, user will have to manually navigate to your apps permission settings if he ever wishes to enable it. – Pawel Oct 16 '20 at 13:13
  • 1
    Yes, I alredy understand it. First step. The app should request foreground location permissions : Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,location Second step. The app should request background location permissions : @RequiresApi(api = Build.VERSION_CODES.Q) private static final String[] BACKGROUND_LOCATION_PERMISSIONS = { Manifest.permission.ACCESS_BACKGROUND_LOCATION, }; Only in this order! If first I try to request background permissions - it will not work. – Vitaly Oct 16 '20 at 13:20
  • 1
    My app requires location permission but never requires or uses location. This is because Android stupidly requires location permissions to SCAN (not transmit) for BLE devices. That means the user will never do anything that needs location permission except to run the app, as the app looks for bluetooth devices - that is what it does. So I am stuck with permission dialogs not being displayed on Android 11 and I only ask for background - not foreground – Brian Reinhold Dec 28 '20 at 20:33
  • when you say one at a time, you mean request the location permissions and handle the result in the activity response. And then ask for background permissions and handle the result in the activity response? – Brian Reinhold Dec 28 '20 at 22:59
  • @BrianReinhold yes you have to execute permission request twice - once for foreground location and if its granted then you can follow up with background request. – Pawel Dec 28 '20 at 23:14
  • Yes, I went ahead and implemented what I guessed you meant and yes indeed, it worked. What a pain. And I don't even use location information or expose it! I just scan for BTLE health devices. – Brian Reinhold Dec 30 '20 at 09:41