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

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

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();

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

boolean 值可用于检查用户之前是否拒绝过权限。

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

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();
}