如何正确处理Android6.0+的运行时权限申请

Android 6.0(API 23) 开始,允许用户在应用运行时向其授予权限,好处有:

  • 简化应用安装过程,无需在安装或更新时授予权限
  • 用户可以对应用的功能进行更多控制;例如,用户可以选择为相机应用提供相机访问权限,而不提供设备位置的访问权限。

1.官方推荐的方法

官方文档里对于如何使用 Android 支持库来检查和请求权限有详细的介绍,以下是相关代码。

// 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.
    }
}
复制代码

处理逻辑是:

  1. 检查是否拥有该权限
  2. 如果是, 进入正常的功能业务处理流程
  3. 如果否:
    • 解释应用为什么需要该权限,然后请求它
    • 直接请求该权限

调用requestPermissions方法申请权限后,系统会立即弹出权限申请对话框。如果用户点击了“允许”按钮,后面的一切都会很顺利。如果用户点击了“拒绝”按钮,下次再申请权限的时候会执行shouldShowRequestPermissionRationale方法对应的代码块。

2.问题

问题的关键在于shouldShowRequestPermissionRationale方法的调用。

  1. 仅在用户已拒绝某项权限请求时,shouldShowRequestPermissionRationale方法才会返回true
  2. 在用户已拒绝某项权限请求之后再次申请该权限时,系统的权限申请对话框会多显示出一个“不再询问”的复选框,如果用户点击了此复选框后,如何处理?

3.改进的方法

改进后的处理逻辑:

  1. 检查是否拥有该权限
  2. 如果是, 进入正常的功能业务处理流程
  3. 如果否,弹出权限使用解释对话框,解释应用为何需要该权限,用户点击“确定”按钮后,请求该权限。

处理权限请求响应:

  • 请求成功(Success case)
  • 请求失败(Failure case)
  • 不再询问(NeverAskAgain case) , 建议在这里弹出自定义对话框,提示该权限已被用户拒绝过,并引导用户进入系统的“Settings”界面授予权限。代码如下:
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", packageName, null))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
复制代码

4.代码实现

4.1 检查权限
if (isPermissionGranted(SEND_SMS)) {
    // Great, got the permission
} else {
    showPermissionReasonAndRequest("Notice","Hi, we will request SEND SMS permission. " +
        "This is required for authenticating your device, please grant it.",
    Manifest.permission.SEND_SMS,requestCode)
}

fun isPermissionGranted(permission:String):Boolean =
    ContextCompat.checkSelfPermission(this,permission) == PackageManager.PERMISSION_GRANTED
复制代码
4.2 弹出权限使用解释对话框
fun Activity.showPermissionReasonAndRequest(title: String,message: String,permission: String,requestCode: Int){
    AlertDialog.Builder(this).setMessage(message)
                .setTitle(title)
                .setPositiveButton("confirm", { _, _ -> ActivityCompat.requestPermissions(this,arrayOf(permission),requestCode) })
                .setNegativeButton("cancel", null)
                .create()
                .show()
}

复制代码
4.3 处理权限请求响应
override fun onRequestPermissionsResult(requestCode: Int, permissions:Array<String>, grantResults: IntArray) {
    
    if (requestCode != expectRequestCode) return
    
    //Success case--Get the permission
    if (grantResults.isNotEmpty()&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Do something and return
        return
    }
    
    //NeverAskAgain case- Never Ask Again has been checked
    if (isUserCheckNeverAskAgain(Manifest.permission.SEND_SMS)) {
        // Do something and return
        return
    }
    
    //Failure case--Not get the permission
    // Do something and return
}

fun Activity.isUserCheckNeverAskAgain(permission:String) =!ActivityCompat.shouldShowRequestPermissionRationale(this,permission)

复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值