获取联系人

时间:2016-09-03 16:57:08

标签: android android-contentprovider android-contacts

任何人都可以建议我在我的代码中进行哪些更改,以便从Android中的联系人列表中检索联系人。我写的逻辑对于棒棒糖和下面的棒棒糖来说是完美的,但是对于上面的棒棒糖,它说崩溃说安全例外..

1 个答案:

答案 0 :(得分:1)

请参阅https://developer.android.com/training/permissions/requesting.html

"从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.
}
}