Android开发

Android权限状态判断

2022-04-27  本文已影响0人  人世看客

一、背景

做啥事肯定是有原因的,最近在做和H5那边交互,那边申请某个权限之前,需要知道该权限状态,其中一个状态,就是判断用户是否永久拒绝了此权限(拒绝且不在提醒),这个就比较坑,iOS那边是有方法直接获取的,但是Android这边貌似没有直接获取此状态的方法(有找到此方法的小伙伴请留言)。我们最熟悉的权限检测方法ActivityCompat.checkSelfPermission(activity, permission),但是此方法只返回true和false,true是已授权,false是用户拒绝了此权限,无法应付需求

二、实现逻辑

1.不会的当然是百度,百度中找到了一个方法ActivityCompat.shouldShowRequestPermissionRationale(activity, permission),此方法也只返回true和false。以下是每种情况返回值

2.找到了此方法,离成功就不远了。现在来说下具体判断逻辑,我们先总结下,一共会出现的几种状态,第一种就是还没申请过此权限,第二种是用户已同意权限,第三种就是申请了权限,被用户拒绝,但下次申请还会弹窗提醒,第四种就是我们要找的,用户拒绝了权限,并且下次不会再提示

三、具体代码

不单独获取状态,包含了申请权限,做了个简单的权限申请封装,目前只做申请单个权限功能,自己可以加多权限处理。说了这么多废话,现在具体上代码( ˘•ω•˘ )

object PermissionUtils {
    //获取权限成功
    const val STATUS_SUCCESS = 0
    //申请权限拒绝, 但是下次申请权限还会弹窗
    const val STATUS_REFUSE = 1
    //申请权限拒绝,并且是永久,不会再弹窗
    const val STATUS_REFUSE_PERMANENT = 2
    //默认未请求授权状态
    const val STATUS_DEFAULT = 3

    private const val REQUEST_CODE = 10000
    private lateinit var permissions: Array<out String>
    private var listener: PermissionListener?= null

    /**
     * 判断是否已授权
     */
    fun isAuthorized(activity: Activity, authorize: String): Boolean{
        val isShow = ActivityCompat.shouldShowRequestPermissionRationale(activity, authorize)
        LogUtils.d("print","$isShow")
        val flag = ActivityCompat.checkSelfPermission(activity, authorize)
        if (flag!= PackageManager.PERMISSION_GRANTED){
            return false
        }
        return true
    }

    /**
     * 获取权限状态
     */
    fun getAuthorizeStaus(activity: Activity,authorize: String): Int{
        val flag = ActivityCompat.checkSelfPermission(activity, authorize)
        val isShould = ActivityCompat.shouldShowRequestPermissionRationale(activity, authorize)
        if (isShould){
            return STATUS_REFUSE
        }
        if (flag == PackageManager.PERMISSION_GRANTED){
            //获取到权限
            return STATUS_SUCCESS
        }
        if (!SharedUtils.contains(authorize)){
            return STATUS_DEFAULT
        }
        return STATUS_REFUSE_PERMANENT
    }

    /**
     * 申请单个权限权限
     */
    fun requestPermission(activity: Activity,authorize: String,listener: PermissionListener){
        this.listener = listener
        permissions = arrayOf(authorize)
        val flag = ActivityCompat.checkSelfPermission(activity, authorize)
        if (flag!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(activity, permissions,REQUEST_CODE)
        }else{
            this.listener?.requestResult(true)
        }
    }

    /**
     * 返回结果
     */
    fun onRequestPermissionsResult(activity: Activity,requestCode: Int, permissions: Array<out String>,  grantResults: IntArray){
        if (requestCode != REQUEST_CODE){
            return
        }
        permissions.forEach {
            val isShould = ActivityCompat.shouldShowRequestPermissionRationale(activity, it)
            SharedUtils.putBoolean(it,isShould)
        }
        grantResults.forEach {

            if (it != PackageManager.PERMISSION_GRANTED){
                listener?.requestResult(false)
                return
            }
        }
        listener?.requestResult(true)
    }
}

interface PermissionListener {
    /**
     * 授权结果
     */
    fun requestResult(isFlog: Boolean)
}

SharedUtils工具类,需要先SharedUtils.init(context)初始化,不然会报错

object SharedUtils {
    const val FILE_NAME = "JsLibrary"
    lateinit var sp: SharedPreferences

    fun init(context: Context){
        sp = context.applicationContext.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
    }
    /**
     * 添加boolean值
     */
    fun putBoolean(key: String,value: Boolean){
        sp.edit().putBoolean(key,value).apply()
    }
    /**
     * 获取boolean值
     */
    fun getBoolean(key: String,default: Boolean): Boolean{
        return sp.getBoolean(key,default)
    }

    /**
     * 判断是否存在
     */
    fun contains(key: String): Boolean{
        return sp.contains(key)
    }
}

具体调用方法

/**
     * 权限返回结果
     */
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionUtils.onRequestPermissionsResult(this,requestCode,permissions,grantResults)
    }
val status = PermissionUtils.getAuthorizeStaus(activity,xxx权限)

status就是返回的那四种状态

PermissionUtils.requestPermission(activity, xx权限, object : PermissionListener{
                    override fun requestResult(isFlog: Boolean) {
                        Log.d("print","$isFlog")
                    }
                })

申请权限回调结果isFlog,true同意权限,false为拒绝权限

上一篇下一篇

猜你喜欢

热点阅读