【问题标题】:Android Marshmallow security exception when getting contacts获取联系人时的 Android Marshmallow 安全异常
【发布时间】:2016-09-03 16:57:07
【问题描述】:

谁能建议我在我的代码中进行哪些更改以从 Android 的联系人列表中检索联系人。我写的逻辑非常适合棒棒糖及以下,但对于棒棒糖上面的情况,它会崩溃说安全异常..

【问题讨论】:

  • 您是否在运行时获得权限?对于 Android M 及以上版本,您需要在运行时请求权限。
  • 我刚刚在清单中请求许可,并使用内容提供者进行检索。
  • 非常感谢您的解决方案。

标签: android android-contentprovider android-contacts


【解决方案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.
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2014-01-21
    相关资源
    最近更新 更多