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 :
STEP 2: The app should request background location permissions :
Only in this order!
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.
p.s. I provided this more clear - in my opinion, answer with code examples, additionally to correct Pawel answer,