Android权限工具

Android权限请求工具

  /**
     * 权限请求判断带有弹窗
     *
     * 使用案例:val perms = arrayOf(
     * Manifest.permission.WRITE_EXTERNAL_STORAGE,
     * Manifest.permission.READ_EXTERNAL_STORAGE,
     * Manifest.permission.CAMERA
     * )
     *
     * permissionGrantedDialog(
     * requireActivity(), //上下文
     * fragment = this, //注:fragment页面调用时候传当前fragment上下文this ,activity页面调用不用传
     * perms, //权限数组
     * title = "权限禁止")//权限禁用后提示弹窗标题
     * {toast("权限通过")}//权限通过后的回调函数处理其他业务
     *
     */
    fun permissionGrantedDialog(
        activity: ComponentActivity,
        fragment: Fragment? = null,
        perms: Array<String>,
        title: String="权限被禁止",
        callback: () -> Unit
    ) {
        permissionGranted(activity, fragment = fragment, perms) {
            when (it) {
                1 -> {//弹出自定义权限弹窗引导用户
                    val alertDialog: AlertDialog = AlertDialog.Builder(activity)
                        .setTitle(title) // 设置标题
                        .setMessage("请前往手动授予该权限") // 设置内容
                        // 添加一个确认按钮并设置点击按钮后执行的代码
                        .setPositiveButton("前往") { _, _ ->
                            toAppSysSetting(activity)
                        }
                        .setNegativeButton("取消", null) // 添加一个取消按钮,无命令执行
                        .create() // 创建对话框
                    alertDialog.show() // 显示对话框
                }
                2 -> {//权限同意,其他操作
                    callback.invoke()
                }
            }
        }
    }

    /**
     * 权限判断
     * granted 0未同意 1被禁止 2同意
     *
     * forActivity 区分registerForActivityResult 是activity还是fragment使用
     */
    fun permissionGranted(
        activity: ComponentActivity,
        fragment: Fragment? = null,
        perms: Array<String>,
        callback: (granted: Int) -> Unit
    ) {
        val denied = checkPermission(activity, perms)//是否拒绝
        val forbid = checkPermissionForbid(activity, perms)//是否禁用

        when {
            denied == true && forbid == false -> {
                //权限未允许,发起请求权限 granted=0 需用 registerForActivityResult launch 发起权限请求
                (fragment ?: activity).registerForActivityResult(
                    ActivityResultContracts.RequestMultiplePermissions()
                ) { maps ->
                    val granted = checkPermissionResult(maps)
                    callback.invoke(
                        when (granted) {
                            true -> 2//权限同意
                            else -> 1//权限禁止 手动弹出
                        }
                    )
                }.launch(perms)
            }
            denied == true && forbid == true -> {
                //权限禁止,弹出自定义弹窗跳转设置引导
                callback.invoke(1)
            }
            else -> {
                //权限允许
                callback.invoke(2)
            }
        }
    }

    /**
     * 检查权限返回结果 权限同意true 否者false
     */
    fun checkPermissionResult(maps: Map<String, Boolean>): Any {
        val granted = run breaking@{
            maps.forEach {
                return@breaking it.value

            }
        }
        return granted
    }

    /**
     * 检查权限是否都同意
     */
    private fun checkPermission(activity: Activity, perms: Array<String>): Any {
        val denied = run breaking@{
            perms.forEach {
                return@breaking ContextCompat.checkSelfPermission(
                    activity,
                    it
                ) != PackageManager.PERMISSION_GRANTED
            }
        }
        return denied
    }

    /**
     * 检查权限是否禁止 弹出自定义弹窗跳转设置
     */
    private fun checkPermissionForbid(activity: Activity, perms: Array<String>): Any {
        val forbid = run breaking@{
            perms.forEach {
                return@breaking activity.shouldShowRequestPermissionRationale(it)
            }
        }
        return forbid
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值