检测是否可以请求许可或永久拒绝许可 [英] Detecting wether a permission can be requested or is permanently denied

查看:20
本文介绍了检测是否可以请求许可或永久拒绝许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况
我的一个片段访问了相机.所以当然我需要先检查我的活动中的权限,然后再重定向到它.如果用户拒绝该权限,则 Activity 将完成并将他重定向到他所在的上一个 Activity.
此外,我想在 Activity 启动并检测到权限被永久拒绝时显示不同的片段.(android 权限对话框上的小复选框不再询问")

Situation
One of my fragments accesses the camera. So of course i need to check the permission in my activity first, before i redirect to it. If the user denies the permission the Activity finishes and redirects him to the previous Activity he was in.
Additionally I would like to show a different fragment when the Activity starts and detects that the permission was permanently denied. (The little checkbox "Never ask again" on the android permission dialog)

问题
我找不到合适的检测方法,是只拒绝了一次权限,还是上次选中了不再询问"复选框并永久拒绝了权限.
请记住,我不想在 onRequestPermissionsResult 回调中知道这一点.我需要在我的 Activity 的 onCreate 中知道当前是否授予、拒绝或永久拒绝权限.

Problem
I could not find a proper way of detecting, wether the permission was only denied once or if the "Never ask again" checkbox was checked last time and denied the permission permanently.
Keep in mind that I don't want to know that in the onRequestPermissionsResult callback. I need to know in the onCreate of my Activity if the permission currently is granted, denied or permanently denied.

我的尝试
ActivityCompat#shouldShowRequestPermissionRationale 似乎可以检测权限过去是否被拒绝.如果仅被拒绝一次而不是永久拒绝,它也会返回 true.

What I tried
ActivityCompat#shouldShowRequestPermissionRationale seems to detect wether the permission has been denied in the past or not. It also returns true if it has been denied only once instead of permanently.

PermissionChecker#checkPermission() 似乎没有注意到永久和仅一次拒绝权限状态之间的任何区别.

PermissionChecker#checkPermission() didn't seem to notice any difference between permanently and only once denied permission state.

问题
有什么办法可以检测,是权限被拒绝但仍然可以请求还是永久拒绝?

Question
Is there any way of detecting, wether a permission is denied but can still be requested or if it is permanently denied?

推荐答案

有什么办法可以检测权限是否被拒绝但可以仍然被要求还是被永久拒绝?

Is there any way of detecting, whether a permission is denied but can still be requested or if it is permanently denied?

遗憾的是,没有官方 API 可用于检测用户选择不再询问"时是否永久拒绝权限

Unfortunately there is no official API available to detect if permission is permanently denied when user selects "Never ask again"

有一种解决方法是使用 shouldShowRequestPermissionRationale.创建一个默认值为 false 的 SharedPreference 并将 shouldShowRequestPermissionRationale 返回的值存储在其中.在更新值之前,请检查设置的值是否为 true.如果它是 true 则不要更新它.

There is one work around which uses shouldShowRequestPermissionRationale. Create a SharedPreference with default value false and store value returned by shouldShowRequestPermissionRationale in it. Before updating the value, check if the value set was true. If it was true then don't update it.

每当您想检查权限时,从 SharedPreference 获取值和 shouldShowRequestPermissionRationale 返回的当前值.如果 shouldShowRequestPermissionRationale 返回 falseSharedPreference 的值为 true,您可以推断 不再询问 被用户选中.

Whenever you want to check for permission, get the value from SharedPreference and current value returned by shouldShowRequestPermissionRationale. If shouldShowRequestPermissionRationale returns false but value from SharedPreference is true, you can deduce that Never ask again was selected by user.

你可以参考我的博客 我在其中描述了这种方法.

You can refer to my blog where I have described this approach.

这篇关于检测是否可以请求许可或永久拒绝许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆