我正在使用 Kotlin 构建 Android 应用程序,并希望使用 PermissionX 库向用户请求运行时权限。
我已经将该库添加到我的项目中并遵循文档,但我不确定如何正确实现它来请求 READ_EXTERNAL_STORAGE 和 CAMERA 等权限。
这是我迄今为止尝试过的:
PermissionX.init(this)
.permissions(android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA)
.onExplainRequestReason { scope, deniedList ->
scope.showRequestReasonDialog(
deniedList,
"The app needs these permissions to work properly. Please grant them.",
"OK",
"Cancel"
)
}
.onForwardToSettings { scope, deniedList ->
scope.showForwardToSettingsDialog(
deniedList,
"You need to allow these permissions in settings manually.",
"OK",
"Cancel"
)
}
.request { allGranted, grantedList, deniedList ->
if (allGranted) {
Toast.makeText(this, "All permissions are granted", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "These permissions are denied: $deniedList", Toast.LENGTH_SHORT).show()
}
}
但是,当我运行该应用程序时,遇到以下问题:
权限请求对话框有时不会出现,应用程序的行为就像权限被拒绝一样,即使我没有与该对话框进行交互。
我想知道:
我相信我对这个问题有正确的答案。
这就是在Android中实现permissionX的方式。
fun requestPermissionIfNeeded(permissions: List<String>, requestCallback: RequestCallback?) {
var allGranted = true
for (permission in permissions) {
if (ContextCompat.checkSelfPermission(
this, permission!!
) != PackageManager.PERMISSION_GRANTED
) {
allGranted = false
}
}
if (allGranted) {
requestCallback!!.onResult(true, permissions, ArrayList())
return
}
PermissionX.init(this).permissions(permissions)
.onExplainRequestReason(ExplainReasonCallback { scope: ExplainScope, deniedList: List<String> ->
var message = ""
if (permissions.size == 1) {
if (deniedList.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
message = this.getString(R.string.permission_explain)
}
} else {
if (deniedList.size == 1) {
if (deniedList.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
message = this.getString(R.string.permission_explain)
}
} else {
message = this.getString(R.string.permission_explain)
}
}
scope.showRequestReasonDialog(deniedList, message, getString(R.string.ok))
})
.onForwardToSettings(ForwardToSettingsCallback { scope: ForwardScope, deniedList: List<String> ->
var message = ""
if (permissions.size == 1) {
if (deniedList.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
message = this.getString(R.string.permission_explain)
}
} else {
if (deniedList.size == 1) {
if (deniedList.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
message = this.getString(R.string.permission_explain)
}
} else {
message = this.getString(R.string.permission_explain)
}
}
scope.showForwardToSettingsDialog(
deniedList, message, getString(R.string.settings), getString(R.string.cancel)
)
}).request(RequestCallback { allGranted, grantedList, deniedList ->
requestCallback?.onResult(
allGranted, grantedList, deniedList
)
})
}
您可以按如下方式实践:
val strings = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
listOf(Manifest.permission.READ_MEDIA_IMAGES) // For 10+
} else {
listOf(Manifest.permission.READ_EXTERNAL_STORAGE) // For below 10
}
requestPermissionIfNeeded(strings) { allGranted, grantedList, deniedList ->
if (allGranted) {
val i = Intent(
Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
startActivityForResult(
i, GALLERY_CODE
)
}
}