我尝试使用新的权限请求方式(允许系统管理权限请求代码),如此处所述https://developer.android.com/training/permissions/requesting#allow-system-manage-request-code。当我点击相机按钮时,我只收到“权限被拒绝”的提示,但我没有得到一个实际的权限请求窗口(在那里我可以允许或拒绝它)。文档中写道:“在必要时显示系统权限对话框,请在上一步保存的ActivityResultLauncher实例上调用launch()方法。”我做错了什么?我错过了什么?
在onViewCreated下面我有:
在onViewCreated下面我有:
binding.camera.setOnClickListener{
when {
ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED -> {
// You can use the API that requires the permission.
//startCamera()
makeText(activity, "Start camera", LENGTH_SHORT).show()
}
shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> {
// In an educational UI, explain to the user why your app requires this
// permission for a specific feature to behave as expected. In this UI,
// include a "cancel" or "no thanks" button that allows the user to
// continue using your app without granting the permission.
makeText(activity, "Camera is necessary to add content.", LENGTH_SHORT).show()
}
else -> {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(
Manifest.permission.CAMERA)
}
}
}
我有这段代码:
// Permission
val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) {
isGranted: Boolean -> if (isGranted){
// Permission Accepted
makeText(activity, "Permission Accepted.", LENGTH_SHORT).show()
}
else {
// Permission Denied
makeText(activity, "Permission Denied.", LENGTH_SHORT).show()
}
}