几天前,代码工作正常,没有权限问题。
我无法在运行时授予权限,在从设置授予权限时也有问题。(App权限详情页).
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
}
谁能告诉我为什么上面不起作用?系统直接在结果上,应用程序将应用程序重定向到设置页面,但我也无法从设置页面授予权限。
Is this OS Specific?有人在使用ColorOS 11时有同样的问题吗?如果我身边缺少什么,请指导我。
设备:OPPO F17 Pro操作系统:Color OS 11, Android 11 Based
注意:
以上代码,三星设备,基于Android 11 (OneUI 3.1),应用程序没有要求运行时,但在重定向设置页面后,我授予位置权限,应用程序工作正常,关于OPPO,我无法从设置页面授予权限。
What tried:
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 ))
}
}
试试这个方法可能对你有帮助
fun checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
var permissions: ArrayList<String> = ArrayList<String>()
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION)
val listPermissionsNeeded: ArrayList<String> = ArrayList()
for (p in permissions) {
var result = ContextCompat.checkSelfPermission(this, p!!)
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p)
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(
this,
listPermissionsNeeded.toTypedArray(),
CODE
)
} else {
next()
}
} else {
next()
}
}
override fun onRequestPermissionsResult(
requestc: Int,
permissions: Array<String>,
grantRes: IntArray
) {
super.onRequestPermissionsResult(requestc, permissions, grantRes);
when (requestc) {
CODE -> {
var isGrant = true
if (grantRes.size > 0) {
for (i in 0 until grantResults.size) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
isGrant = false
}
}
}
if (isGrant) {
next()
} else {
checkRationalePermission(permissions)
}
}
}
}
var alertDialogRatinal: android.app.AlertDialog? = null
fun checkRationale(permissions: Array<String>) {
if (alertDialogRatinal != null && alertDialogRatinal!!.isShowing || permissions.size == 0) {
return
}
var someDenied = false
for (permission in permissions) {
if (ActivityCompat.shouldShowRequestPermissionRationale(
this@Activity,
permission!!
)
) {
} else {
if (ActivityCompat.checkSelfPermission(
this@Activity,
permission
) == PackageManager.PERMISSION_GRANTED
) {
} else {
someDenied = true
}
}
}
if (someDenied) {
val alertDialogBuilder =
android.app.AlertDialog.Builder(this@Activity)
alertDialogRatinal = alertDialogBuilder.setTitle("Permissions Required")
.setMessage(
"Please open settings, go to permissions and allow them."
)
.setPositiveButton(
"Settings"
) { dialog, which ->
val intent = Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts(
"package",
this@Activity.getPackageName(),
null
)
)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivityForResult(intent, 100)
}
.setNegativeButton(
"Cancel"
) { dialog, which -> }
.setCancelable(false)
.create()
alertDialogRatinal!!.show()
}
}
系统重置修复了我的问题,但根据评论和文件,请遵循以下位置权限指南。
感谢Pawel的评论,
裁判:https://stackoverflow.com/a/66321942/9909365
后台位置权限不像其他权限那样工作。这是一个将位置许可从前景提升到前景的请求;背景。
用户必须有意识地选择"允许"。为了做到这一点并授予后台位置许可。否则,认为该权限被拒绝。
你甚至不能请求后台位置,除非前台位置已经被授予-当系统权限活动显示时,它应该已经选择了选项2或3。
看到https://developer.android.com/training/location/permissions request-background-location
注意:
当您的应用程序使用此后台位置时,请准备一个简短的视频,演示您的应用程序中需要在后台访问位置的基于位置的功能(当应用程序未使用时)
见https://support.google.com/googleplay/android-developer/answer/9799150
private val requestIdMultiplePermissions = 1
private val permissionsRequest: ArrayList<String> =
arrayListOf(READ_CALENDAR
, CAMERA)
findViewById<Button>(R.id.multiplePermissionBtn).setOnClickListener {
if (checkMultipleRequestPermissions()) {
doOperation()
}
}
private fun doOperation() {
Toast.makeText(this, "Successfully granted", Toast.LENGTH_LONG).show()
}
private fun checkMultipleRequestPermissions(): Boolean {
val listPermissionsNeeded: MutableList<String> = ArrayList()
for (p in permissionsRequest) {
val result = ContextCompat.checkSelfPermission(this, p)
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p)
}
}
if (listPermissionsNeeded.isNotEmpty()) {
ActivityCompat.requestPermissions(
this,
listPermissionsNeeded.toTypedArray(),
requestIdMultiplePermissions
)
return false
}
return true
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == requestIdMultiplePermissions) {
if (grantResults.isNotEmpty()) {
var isGrant = true
for (element in grantResults) {
if (element == PackageManager.PERMISSION_DENIED) {
isGrant = false
}
}
if (isGrant) {
doOperation()
} else {
var someDenied = false
for (permission in permissions) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(
this,
permission
)
) {
if (ActivityCompat.checkSelfPermission(
this,
permission
) == PackageManager.PERMISSION_DENIED
) {
someDenied = true
}
}
}
if (someDenied) {
settingActivityOpen()
}else{
showDialogOK { _: DialogInterface?, which: Int ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> checkMultipleRequestPermissions()
DialogInterface.BUTTON_NEGATIVE -> { }
}
}
}
}
}
}
}
private fun settingActivityOpen() {
Toast.makeText(
this,
"Go to settings and enable permissions",
Toast.LENGTH_LONG
)
.show()
val i = Intent()
i.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
i.addCategory(Intent.CATEGORY_DEFAULT)
i.data = Uri.parse("package:$packageName")
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
startActivity(i)
}
private fun showDialogOK(okListener: DialogInterface.OnClickListener) {
MaterialAlertDialogBuilder(this)
.setMessage("All Permission required for this app")
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show()
}