ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-Android targetSdkVersion 23 checkSelfPermission方法

2019-10-27 08:26:01  阅读:263  来源: 互联网

标签:android-permissions android-contacts java android


我正在检查Android 6.0(API 23)中的权限值.即使从应用程序设置启用或禁用权限,也始终获得0值.

以下是我采取的步骤.

联系人权限从设备设置->应用程序->中手动禁用我的应用程序->许可->禁用联系人权限.

在代码行下面执行时,仍然在Android 6.0中每次都得到0值.

ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)

下面是我的代码.我在主启动器活动类中定义的

// Identifier for the permission request
private static final int WRITE_CONTACTS_PERMISSIONS_REQUEST = 9;
........

@Override
public void onCreate(Bundle savedInstanceState) {
....
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.LOLLIPOP_MR1)
    {
        sharedPreferencesEditor.putBoolean(getString(R.string.ALLOW_ACCESS_PHONEBOOK), true);
        sharedPreferencesEditor.commit();
    }
    else {
        getPermissionToReadUserContacts();
    }
....

}


// Called when the user is performing an action which requires the app to read the
// user's contacts
public void getPermissionToReadUserContacts() {
    // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
    // checking the build version since Context.checkSelfPermission(...) is only available
    // in Marshmallow
    // 2) Always check for permission (even if permission has already been granted)
    // since the user can revoke permissions at any time through Settings
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {

        // The permission is NOT already granted.
        // Check if the user has been asked about this permission already and denied
        // it. If so, we want to give more explanation about why the permission is needed.
        if (shouldShowRequestPermissionRationale(
                Manifest.permission.WRITE_CONTACTS)) {
            // Show our own UI to explain to the user why we need to read the contacts
            // before actually requesting the permission and showing the default UI
        }

        // Fire off an async request to actually get the permission
        // This will show the standard permission request dialog UI
        requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS},
                WRITE_CONTACTS_PERMISSIONS_REQUEST);
    }
}

// Callback with the request from calling requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[],
                                       @NonNull int[] grantResults) {
    // Make sure it's our original READ_CONTACTS request
    if (requestCode == WRITE_CONTACTS_PERMISSIONS_REQUEST) {
        if (grantResults.length == 1 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Write Contacts permission granted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Write Contacts permission denied", Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

解决方法:

如果您以SDK 23(Android 6)为目标,则默认情况下(清单中的)所有权限都处于禁用状态,而如果targetSDK是22(Android 5.1),并且您的应用程序在Android 6上运行,则所有权限都可以通过启用用户安装应用程序时的默认设置,即使用户稍后撤消了权限,checkSelfPermission也会返回PERMISSION_GRANTED的错误值

也可以在PermissionChecker的文档中找到

In the new permission model permissions with protection level dangerous are runtime permissions. For apps targeting M and above the user may not grant such permissions or revoke them at any time. For apps targeting API lower than M these permissions are always granted as such apps do not expect permission revocations and would crash. Therefore, when the user disables a permission for a legacy app in the UI the platform disables the APIs guarded by this permission making them a no-op which is doing nothing or returning an empty result or default error.

标签:android-permissions,android-contacts,java,android
来源: https://codeday.me/bug/20191027/1942865.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有