checkSelfPermission使用targetSdkVersion <= 22返回PERMISSION_GRANTED以获取已撤销的权限

Ven*_*r85 18 android android-6.0-marshmallow

我正在研究Android Marshmallow的新权限模型,但我面临一个我觉得奇怪的问题.

一个targetSdkVersion22 的应用程序(尚未使用Android Marshmallow的新权限模型)声明READ_CONTACTS清单中的权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />
Run Code Online (Sandbox Code Playgroud)

并尝试通过以下方式阅读联系人的电话号码Intent.ACTION_PICK:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT_REQUEST);
Run Code Online (Sandbox Code Playgroud)

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

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在尝试操作之前,如何在许可之前明确地检测用户是否明确?

我还尝试了https://github.com/googlesamples/android-RuntimePermissions上的官方Google示例,将targetSdkVersion结果设置为22,结果相同:即使用户撤消了权限,它也会记录授予权限,然后操作失败.

谢谢 ;)