Android M-无法检查运行时权限 [英] Android M - failed to check runtime permission

查看:162
本文介绍了Android M-无法检查运行时权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have just started working with runtime permissions on Android M. I have CAMERA, READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE declared in AndroidManifest.xml

问题是,如果用户转到设置"并按如下所示明确关闭权限,则我无法检查我的应用程序是否启用了摄像头"或存储"权限

The thing is if user go to Settings and explicitly turn off permissions as below, I cannot check if my app that has Camera or Storage permissions enabled or not

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

I used this snippet to check if my app has a particular permission or not:

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(已授予).

Surprisingly, every check with Camera and Storage permissions returns true (granted) even I turned them off in my Settings.

我的targetSdkVersion是23

这是Google提供的预期行为吗?我环顾了网上,但仍然找不到任何文档说明有关处理显式调用的权限的任何信息.

Is this expected behavior provided by Google? I looked around the web and still could not find any documentation say anything about handle explicitly-invoked permissions.

推荐答案

再次在onRestart()中检查权限.

Again check for permission in 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/

Check out this link for further information: https://github.com/sagarjogadia28/PermissionSample/

这篇关于Android M-无法检查运行时权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆