Android M - 无法检查运行时权限

时间:2016-06-30 10:56:53

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

我刚开始使用Android M上的运行时权限。我在AndroidManifest.xml中声明了CAMERA,READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE

问题是,如果用户转到“设置”并明确关闭以下权限,我无法检查我的应用是否启用了相机或存储权限

enter image description here

我使用此代码段来检查我的应用是否拥有特定权限:

public static boolean hasPermission(Context c, String permission) {
    if (Build.VERSION.SDK_INT >= 23) {
        return ContextCompat.checkSelfPermission(c, permission) == PackageManager.PERMISSION_GRANTED;
    }
    return true;
}

令人惊讶的是,即使我在“设置”中将其关闭,每次使用“相机”和“存储”权限检查都会返回true(已授予)。

我的targetSdkVersion是23

这是Google提供的预期行为吗?我环顾网络,仍然找不到任何关于处理显式调用权限的文档。

4 个答案:

答案 0 :(得分:1)

再次检查onRestart()的权限。

@TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onRestart() {
        super.onRestart();
        checkForPermission();
    }


@TargetApi(Build.VERSION_CODES.M)
    private void checkForPermission() {
        int permissionCheck = checkSelfPermission(Manifest.permission.READ_CONTACTS);
        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "Granted");
        } else {
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_CONTACTS)) {
                Log.d(TAG, "Contacts Permission Required!!");
                createSnackbar("Contacts Permission Required!!", "Try Again");
            }
            ActivityCompat.
                    requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CONTACT);

        }
    }

查看此链接以获取更多信息: https://github.com/sagarjogadia28/PermissionSample/

答案 1 :(得分:1)

您必须更改targetSDKVersion,因为

  

您应该为23以下的任何targetSdkVersion获取PERMISSION_GRANTED,即使用户在“设置”中切换了该权限组也是如此。我建议您完全卸载应用程序,将targetSdkVersion设置为23,然后再次尝试。

您可以查看此link

答案 2 :(得分:0)

你需要做不同的事情。

首先,请求许可您要执行的操作。示例摄像头:

//This is a class variable
private final static int MY_PERMISSIONS_REQUEST_CAMERA = 1;

...

// before use camera:
// check permissions for CAMERA
if (ContextCompat.checkSelfPermission(ActivityName.this,
        Manifest.permission.CAMERA)
        != PackageManager.PERMISSION_GRANTED) {

    //request permission
    ActivityCompat.requestPermissions(ActivityName.this,
            new String[]{Manifest.permission.CAMERA},
            MY_PERMISSIONS_REQUEST_CAMERA);

} else {
    //permissions granted, do wahtever with camera
    functionToHandleCamera();
}

然后你需要处理用户响应:允许或拒绝。所以覆盖onRequestPermissionsResult方法

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

            } else {
                // permission denied
                Toast msg = Toast.makeText(this, "PERMISSIONS NEEDED", Toast.LENGTH_LONG);
                msg.setGravity(Gravity.CENTER, 0, 0);
                msg.show();
            }
            return;
        }

    }
}
  

更多信息:   https://developer.android.com/training/permissions/requesting.html

编辑:如果您只想检查是否授予了权限,那么您需要我编写的代码的第一部分。

答案 3 :(得分:0)

onCreate活动方法中添加此内容: -

if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    0);

        }
    }

override此方法: -

@Override

public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 0: {
            // 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
    }
}