如何区分用户是第一次请求权限还是点击了“不再询问”按钮?

发布于 2025-01-10 06:23:52 字数 1192 浏览 3 评论 0原文

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (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.
    } else {
        if (requireContext().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
            openCamera();
        else {
            //How can I know here if this is was the first time to request the permission or he pressed on Never Ask Again button before?
            //Run this code if that was the first time to request the permission -> requestPermissionLauncher.launch(Manifest.permission.CAMERA);
            //Run this code if he pressed on Never Ask Again button -> new AlertDialog(context)... etc - The dialog contains a button that moves the user to app settings to enable the permission
        }
    }
} else
    openCamera();

如何区分用户是第一次请求权限还是点击了“不再询问”按钮?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (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.
    } else {
        if (requireContext().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
            openCamera();
        else {
            //How can I know here if this is was the first time to request the permission or he pressed on Never Ask Again button before?
            //Run this code if that was the first time to request the permission -> requestPermissionLauncher.launch(Manifest.permission.CAMERA);
            //Run this code if he pressed on Never Ask Again button -> new AlertDialog(context)... etc - The dialog contains a button that moves the user to app settings to enable the permission
        }
    }
} else
    openCamera();

How can I distinguish if the user request the permission for the first time or he clicked on the Never Ask Again button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2025-01-17 06:23:52

布尔值可用于检查用户是否先前拒绝了权限。

将以下方法添加到上述文件或创建实用程序文件。

public static boolean neverAskAgainSelected(final Activity activity, final String permission) {
        final boolean prevShouldShowStatus = getRatinaleDisplayStatus(activity,permission);
        final boolean currShouldShowStatus = activity.shouldShowRequestPermissionRationale(permission);
        return prevShouldShowStatus != currShouldShowStatus;
    }

    public static void setShouldShowStatus(final Context context, final String permission) {
        SharedPreferences genPrefs = context.getSharedPreferences("GENERIC_PREFERENCES", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = genPrefs.edit();
        editor.putBoolean(permission, true);
        editor.commit();
    }    public static boolean getRatinaleDisplayStatus(final Context context, final String permission) {
       SharedPreferences genPrefs =     context.getSharedPreferences("GENERIC_PREFERENCES", Context.MODE_PRIVATE);
        return genPrefs.getBoolean(permission, false);
    }

权限请求地方里面调用:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (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.
    } else {
        if (requireContext().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
            openCamera();
        else {
            if(neverAskAgainSelected(this,Manifest.permission.CAMERA){
             // new AlertDialog(context)...
             }
             else{
                requestPermissionLauncher.launch(Manifest.permission.CAMERA);
                }             
            
        }
    }
} else{
    openCamera();
}

A boolean value can be used to check if the user has denied permission earlier or not.

Add the following methods to the above file or create a utility file.

public static boolean neverAskAgainSelected(final Activity activity, final String permission) {
        final boolean prevShouldShowStatus = getRatinaleDisplayStatus(activity,permission);
        final boolean currShouldShowStatus = activity.shouldShowRequestPermissionRationale(permission);
        return prevShouldShowStatus != currShouldShowStatus;
    }

    public static void setShouldShowStatus(final Context context, final String permission) {
        SharedPreferences genPrefs = context.getSharedPreferences("GENERIC_PREFERENCES", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = genPrefs.edit();
        editor.putBoolean(permission, true);
        editor.commit();
    }    public static boolean getRatinaleDisplayStatus(final Context context, final String permission) {
       SharedPreferences genPrefs =     context.getSharedPreferences("GENERIC_PREFERENCES", Context.MODE_PRIVATE);
        return genPrefs.getBoolean(permission, false);
    }

Call inside the permission Request place:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (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.
    } else {
        if (requireContext().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
            openCamera();
        else {
            if(neverAskAgainSelected(this,Manifest.permission.CAMERA){
             // new AlertDialog(context)...
             }
             else{
                requestPermissionLauncher.launch(Manifest.permission.CAMERA);
                }             
            
        }
    }
} else{
    openCamera();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文