checkSelfPermission返回PERMISSION_GRANTED以获得targetSdkVersion< = 22的吊销权限 [英] checkSelfPermission returning PERMISSION_GRANTED for revoked permission with targetSdkVersion <= 22

查看:277
本文介绍了checkSelfPermission返回PERMISSION_GRANTED以获得targetSdkVersion< = 22的吊销权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Android Marshmallow的新权限模型,但遇到一个奇怪的问题.

I'm studying the new permission model of Android Marshmallow but I'm facing an issue I find odd.

具有targetSdkVersion 22(因此尚未使用Android Marshmallow的新权限模型)的应用在清单中声明READ_CONTACTS权限:

An app with targetSdkVersion 22 (so not yet using the new permission model of Android Marshmallow) declares the READ_CONTACTS permission in the manifest:

<uses-permission android:name="android.permission.READ_CONTACTS" />

并尝试通过Intent.ACTION_PICK读取联系人的电话号码:

and tries to read the phone number of a contact via Intent.ACTION_PICK:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT_REQUEST);

在具有棉花糖MRA58K的设备上运行时,我通过ADB安装应用程序后撤消了权限后,ContextCompat.checkSelfPermission()方法仍然返回PERMISSION_GRANTED,但是稍后访问联系人时操作失败,因为没有记录的游标返回.据我了解,这是避免旧应用崩溃的默认可逆兼容性"策略.

When running on a device with Marshmallow MRA58K, after I revoke the permission after installing the app via ADB, the ContextCompat.checkSelfPermission() method still returns PERMISSION_GRANTED, but the operation fails later when accessing the contacts because a cursor with no records is returned. As I understood, this is the default "retrocompatibility" strategy to avoid legacy app to crash.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACT_REQUEST && resultCode == Activity.RESULT_OK) {
        Log.i("", "Permission granted: " + (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED));
        Uri contactUri = data.getData();
        Cursor c = null;
        try {
            c = getContentResolver().query(contactUri, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, null);
            if (c != null && c.moveToFirst()) {
                Log.i("", "got phone number: " + c.getString(0));
            } else {
                Log.w("", "No data received");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (c != null) {
                c.close();
            }
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

在尝试操作之前,如何安全地检测用户是否在权限之前?

How can I safely detect if the user explicitly before the permission before attempting the operation?

我还在 https://github.com/googlesamples/android-RuntimePermissions targetSdkVersion设置为22,结果相同:即使用户撤销了权限,它也会记录授予该权限的权限,这样操作就会失败.

I also tried the official Google sample at https://github.com/googlesamples/android-RuntimePermissions setting the targetSdkVersion to 22 with the same result: it logs that the permission is granted even if the user revoked it, ant then the operation fails.

谢谢;)

推荐答案

使用android.support.v4.content.PermissionChecker

Use the android.support.v4.content.PermissionChecker

https://developer.android.com/reference/android/support/v4/content/PermissionChecker.html

这篇关于checkSelfPermission返回PERMISSION_GRANTED以获得targetSdkVersion&lt; = 22的吊销权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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