代码拿走,好用记得点赞一下啊
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)
}
}
}