Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

android - ActivityCompat.requestPermissions for targetSdkVersion 30 is not working

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...