gpt4 book ai didi

android - shouldShowRequestPermissionRationale 总是返回 false

转载 作者:行者123 更新时间:2023-12-01 05:54:44 28 4
gpt4 key购买 nike

目前我正在编写一个读取用户电话号码的安卓应用程序。因为该应用程序是为 SDK 23+(目标 sdk 27​​)开发的,所以我必须请求读取短信权限。如果用户第一次拒绝该权限,则应出现一个对话框,解释电话号码的使用。然后用户可以选择再次请求权限或手动输入电话号码。如果用户勾选“不再询问”,则会出现一个对话框,指示用户通过设置允许权限。

为了检查用户是否勾选了不再询问,我使用了 shouldShowRequestPermissionRationale 方法。但是该方法总是返回 false,即使我从来没有打勾也不会再问。

以下是相关代码:

public class SettingsSettingsFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback {

public void readNumber() {

if (person.getPhoneNumber() == null || person.getPhoneNumber().equalsIgnoreCase("")) {
if (checkSelfPermission(getActivity(), READ_SMS) != PackageManager.PERMISSION_GRANTED)
requestPermission();
else {
//read phoneNumber
}
}
//request permission send sms
private void requestPermission() {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, PERMISSION_REQUEST_SEND_SMS);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_SEND_SMS)
if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// user rejected the permission
boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS);
if (!showRationale) {
// user also CHECKED "never ask again" - show dialog
//show dialog: please allow in settings
} else if (counter < 2) {
// explain the permission, and give the user the possibility to ask once more
counter++;
}
}

为什么 shouldShowRequestPermissionRationale 总是返回 false?
代码来自这里: Android M - check runtime permission - how to determine if the user checked "Never ask again"?
提前感谢您的帮助。

最佳答案

根据 shouldShowRequestPermissionRationale() 的 android 开发者文档:

method returns true if the app has requested this permission previously and the user denied the request. If the user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false



如果您之前没有请求权限,它将返回 false,因为不需要弹出理性警报对话框。

This answer对我理解这个问题很有帮助。

关于android - shouldShowRequestPermissionRationale 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50541186/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com