Android 权限请求封装 拒绝不在访问处理

代码拿走,好用记得点赞一下啊

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistryOwner
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

/**
 * 多权限请求
 *
 * @author HuKui
 * @date 2023/7/26 上午 09:52
 */
class PermissionsRequest(
    private val context: Context,
    permissions: Array<String>,
    private val callBack: PermissionCallBack?
) : ActivityResultCallback<Map<String, Boolean>> {
    private var launcher: ActivityResultLauncher<Array<String>>? = null

    init {
        if (context is ActivityResultRegistryOwner) {
            val owner = context as ActivityResultRegistryOwner
            val registry = owner.activityResultRegistry
            val key = "multiple_permissions"
            registry.register(
                key,
                ActivityResultContracts.RequestMultiplePermissions(),
                this
            ).also { launcher = it }
            launcher?.launch(permissions)
        }
    }

    /**
     * 是否拒绝不在询问
     *
     * @param deniedPermissions 拒绝的权限
     */
    private fun areAllPermissionsNeverAskAgain(deniedPermissions: List<String>): Boolean {
        for (permission in deniedPermissions) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(
                    (context as Activity),
                    permission
                )
            ) {
                return true
            }
        }
        return false
    }

    override fun onActivityResult(result: Map<String, Boolean>) {
        // 未授权
        val deniedPermissions: MutableList<String> = ArrayList()
        for ((permission, granted) in result) {
            if (!granted) {
                deniedPermissions.add(permission)
            }
        }
        if (deniedPermissions.isEmpty()) {
            callBack?.grantPermission()
        } else {
            val deniedPermissionsArray = deniedPermissions.toTypedArray<String>()
            if (areAllPermissionsNeverAskAgain(deniedPermissions)) {
                callBack?.turnedDownAndNotAsk(deniedPermissionsArray)
            } else {
                callBack?.turnedDownPermission(deniedPermissionsArray)
            }
        }
        launcher!!.unregister()
    }

    interface PermissionCallBack {
        /**
         * 用户已授予权限
         */
        fun grantPermission()

        /**
         * 用户已拒绝过权限
         *
         * @param permission:被拒绝的权限
         */
        fun turnedDownPermission(permission: Array<String>?)

        /**
         * 用户已拒绝过并且已勾选不再询问选项、用户第一次申请权限;
         *
         * @param permission:被拒绝的权限
         */
        fun turnedDownAndNotAsk(permission: Array<String>?)
    }

    companion object {
        /**
         * 检测权限
         *
         * @param context    上下文
         * @param permission 要检测的权限
         * @return true:已授权; false:未授权;
         */
        @JvmStatic
        fun isCheckPermission(context: Context, permission: String): Boolean {
            return ContextCompat.checkSelfPermission(
                context,
                permission
            ) == PackageManager.PERMISSION_GRANTED
        }

        @JvmStatic
        fun isCheckPermission(context: Context, permissions: Array<String>?): Array<String>? {
            val list: MutableList<String> = ArrayList()
            if (permissions != null) {
                for (permission in permissions) {
                    if (!isCheckPermission(context, permission)) {
                        list.add(permission)
                    }
                }
            }
            return if (list.size > 0) list.toTypedArray<String>() else null
        }

        /**
         * 请求权限
         *
         * @param context     上下文
         * @param permissions 请求的权限
         * @param callBack    请求的权限结果回调
         */
        @JvmStatic
        fun requestPermission(
            context: Context,
            permissions: Array<String>,
            callBack: PermissionCallBack?
        ) {
            PermissionsRequest(context, permissions, callBack)
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值