Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取联系人时出现Android棉花糖安全异常_Android_Android Contentprovider_Android Contacts - Fatal编程技术网

获取联系人时出现Android棉花糖安全异常

获取联系人时出现Android棉花糖安全异常,android,android-contentprovider,android-contacts,Android,Android Contentprovider,Android Contacts,有谁能建议我在Android中从联系人列表中检索联系人的代码中应该做哪些更改。我所写的逻辑对于棒棒糖和以下的产品来说是完美的,但是对于上面的棒棒糖,它会崩溃,说安全异常 请参阅 “从Android 6.0(API级别23)开始,用户在应用程序运行时授予应用程序权限,而不是在安装应用程序时。” 权限分为正常权限和危险权限 以下代码检查应用程序是否具有读取用户联系人的权限,并在必要时请求该权限: // Here, thisActivity is the current activity if (Co

有谁能建议我在Android中从联系人列表中检索联系人的代码中应该做哪些更改。我所写的逻辑对于棒棒糖和以下的产品来说是完美的,但是对于上面的棒棒糖,它会崩溃,说安全异常

请参阅

“从Android 6.0(API级别23)开始,用户在应用程序运行时授予应用程序权限,而不是在安装应用程序时。”

权限分为正常权限和危险权限

以下代码检查应用程序是否具有读取用户联系人的权限,并在必要时请求该权限:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

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

    // 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 {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}
}

您是否在运行时获得权限?对于Android M和更高版本,您需要在运行时请求权限。我刚刚在清单中请求了权限,并使用内容提供商进行了检索。这将帮助您非常感谢此解决方案。