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

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

问题描述

情况
我的一个片段访问了相机.因此,当然,在重定向到该活动之前,我需要先检查其活动中的权限.如果用户拒绝该权限,则活动结束,并将其重定向到他之前所在的上一个活动.
另外,我想在活动启动并检测到该权限被永久拒绝时显示一个不同的片段. (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?

不幸的是,当用户选择"不再询问"

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返回false,但SharedPreference中的值为true,则可以推断出用户已选择Never ask again.

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天全站免登陆