Goltsev Eugene
Goltsev Eugene

Reputation: 3627

Android Studio add permission check bug?

Ok, I have the following code and warning in AS: enter image description here

When I agree to add such check, AS insert following code:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return TODO;
        }

And I'm just wondering, why AND operator is using?
If user reject only one of permissions above, such check wouldn't be working, as for me.

Or I miss something?

Upvotes: 0

Views: 3176

Answers (2)

Eldho Paul Konnanal
Eldho Paul Konnanal

Reputation: 600

You dont need to check both these permissions. Even if only fine location access is denied ,the other one is automatically denied and vice versa. Bothe comes in the same group- LOCATION http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

Upvotes: 1

Jemshit
Jemshit

Reputation: 10038

You can get location in different ways. Getting it from GPS using LocationManager.GPS_PROVIDER, from network using LocationManager.NETWORK_PROVIDER and from LocationManager.PASSIVE_PROVIDER (GPS).

permission.ACCESS_FINE_LOCATION is needed for GPS_PROVIDER and PASSIVE_PROVIDER.

permission.ACCESS_COARSE_LOCATION is needed for NETWORK_PROVIDER

According to doc:

Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)

So probably Android Studio does not check if you are using both, so it plays safe and checks for both

Upvotes: 1

Related Questions