【发布时间】:2021-10-05 07:45:29
【问题描述】:
几天前,代码运行良好,没有权限问题。
我无法在运行时授予权限,在从设置授予权限时也遇到问题。 (应用权限详情页面)。
var permissions = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
}
在相应的按钮单击时请求权限。
permissionResultLauncher =
registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
var allPermissionGranted = true
permissions.entries.forEach {
if (!it.value) {
Log.e(TAG, "Permission ${it.key} granted : ${it.value}")
allPermissionGranted = false
}
}
if (!permissionDeniedDialog && !allPermissionGranted) {
showDialog(
"Required",
"App needs Bluetooth and Location permissions to scan bluetooth devices.",
"Later",
"Allow",
object : DialogView.ButtonListener {
override fun onNegativeButtonClick(dialog: AlertDialog) {
dialog.dismiss()
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", requireActivity().applicationContext.packageName, null))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
override fun onPositiveButtonClick(dialog: AlertDialog) {
dialog.dismiss()
requestRequirdPermissions()
}
})
} else {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", requireActivity().applicationContext.packageName, null))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
permissionDeniedDialog = true
}
permissionResultLauncher.launch(permissions)
权限检查功能
private fun checkPermission(): Boolean {
for (permission in permissions) {
if (ContextCompat.checkSelfPermission(requireContext(), permission) != PackageManager.PERMISSION_GRANTED) return false
}
return true
}
有人可以告诉我为什么上述方法不起作用吗?系统直接在结果上,应用程序正在将应用程序重定向到设置页面,但我也无法从设置页面授予权限。
这是特定于操作系统的吗?有人遇到与 ColorOS 11 相同的问题吗? 如果我身边缺少任何东西,请指导我。
设备:OPPO F17 Pro 操作系统:Color OS 11,基于 Android 11
注意:
以上代码,使用三星设备,基于 Android 11(OneUI 3.1),应用程序不要求运行时,但在设置页面上重定向后,我授予位置权限并且应用程序工作正常,关于 OPPO 我无法授予权限从设置页面。
尝试了什么:
var permissions = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_COARSE_LOCATION)
}
private fun requestRequirdPermissions() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
permissionResultLauncher.launch(arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION))
}
else
{
permissionResultLauncher.launch(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION ))
}
}
【问题讨论】:
-
“几天前,代码运行良好,没有权限问题”。那么从那以后你有什么改变呢?
-
什么都没变,和以前一样,这就是我来这里的原因,因为我从2小时就开始检查了。
-
您不能一次请求所有不同的位置权限。阅读文档。
-
谢谢,但不确定,我已经尝试使用名为 ACCESS_BACKGROUND_LOCATION 的单一权限仍然无法正常工作。
-
@Ashvinsolanki 您必须逐步请求位置许可:请求前景 (
ACCESS_FINE_LOCATION) 然后在获得许可后您可以请求背景 (ACCESS_BACKGROUND_LOCATION)。如果您在没有先授予前景的情况下要求背景或同时要求两者,则请求会立即被拒绝,甚至不会显示对话框。见stackoverflow.com/questions/66321232/…
标签: android kotlin permissions oppo