android marshmallow:如何知道用户是否拒绝一个应用权限?

时间:2016-09-29 08:50:16

标签: android android-6.0-marshmallow android-permissions

在Android上,我检查位置是否已启用

    $scope.image_change_right = function () {
    if ($scope.picture_value < ($scope.pictures.length-1))
    {
       $scope.picture_value = $scope.picture_value + 1;
       $scope.picture = $scope.pictures[$scope.picture_value];
       console.log($scope.picture_value);
    }
    else{
        $scope.picture_value = 0;
        $scope.picture = $scope.pictures[$scope.picture_value];
        console.log($scope.picture_value);
    }
}

这项工作正常,但是在棉花糖(和鞋面)上,当用户进入应用程序设置并拒绝我的应用程序使用该位置的权限(仅适用于我的应用程序,如mashmallow现在允许这样做)然后是之前的请求仍然返回LocationManager.isProviderEnabled(GPS_PROVIDER) || LocationManager.isProviderEnabled(NETWORK_PROVIDER)

我也试着:

true

但即使用户拒绝我的应用程序的权限

,它也总是返回true

4 个答案:

答案 0 :(得分:1)

确保 build.gradle 中的compileSdkVersiontargetSdkVersion设置为23。如果它低于23,则应用程序将使用旧的权限方法,并且您提到的方法将无效。

答案 1 :(得分:1)

尝试使用以下代码:

ContextCompat.checkSelfPermission(thisActivity,   Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED

答案 2 :(得分:1)

请参阅此link。确保compileSdkVersion应为23岁及以上。

这里我创建了代码,并使用should show rational来检查多个运行时权限。

答案 3 :(得分:0)

checkSelfPermission不返回布尔值。它返回一个整数。

这是检查权限的正确方法:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {

    // permission is not granted, request it
    ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

} else {    
    //permision granted
}

这是处理它们的方式

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

来自文档

  

https://developer.android.com/training/permissions/requesting.html