Android Studio 添加权限检查错误?

Android Studio add permission check bug?

好的,我在 AS 中有以下代码和警告:

当我同意添加此类检查时,AS 插入以下代码:

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 运算符?
如果用户仅拒绝上述权限之一,则此类检查将无法正常工作,对我来说。

还是我漏掉了什么?

您可以通过不同的方式获取位置信息。使用 LocationManager.GPS_PROVIDER 从 GPS 获取,使用 LocationManager.NETWORK_PROVIDER 从网络获取,从 LocationManager.PASSIVE_PROVIDER (GPS) 获取。

permission.ACCESS_FINE_LOCATION 需要 GPS_PROVIDERPASSIVE_PROVIDER

permission.ACCESS_COARSE_LOCATION 需要 NETWORK_PROVIDER

根据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.)

所以可能 Android Studio 不会检查您是否同时使用两者,因此它会安全地检查两者

您不需要检查这两个权限。即使只有精细位置访问被拒绝,另一个也会自动被拒绝,反之亦然。 Bothe来自同一组-位置 http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous