Deny permission on Splash screen but again ask for allow permission prompt in another activity

落花浮王杯 提交于 2019-12-08 11:34:52

问题


if (Build.VERSION.SDK_INT >= 23) {
    if (checkPermission()) {
        Log.e("permission", "Permission already granted.");
    } else {
        requestPermission();
    }
}

private boolean checkPermission() {
    int result = ContextCompat.checkSelfPermission(SolutionBrouchereActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (result == PackageManager.PERMISSION_GRANTED) {
        viewOrDownloadPDF();
        return true;
    } else {
        return false;
    }
}

private void requestPermission() {
    ActivityCompat.requestPermissions(getParent(), new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
}

This functions not worked after denying permission on the splash screen and not able to open permission prompt dialog in another activity.


回答1:


You can call the shouldShowRequestPermissionRationale method in your new Activity if user did not grant the permission on Splash Screen.

Reference: https://developer.android.com/training/permissions/requesting




回答2:


If your target SDK version is >=23 then you need to ask for permissions at run time. Otherwise Android will not ask for permission like old Android does.

If this is the case then you should be able to see that no permissions have been granted if you go to Settings > Apps > "Your app" > Permissions.

If you don't want to ask permissions you can reduce your target sdk version to 22 to get the old permission system. You can still compile with sdk version 23 though.

So for >=23 Use this code:

This code will automatically detect which dialog should be shown, because there is a particular limit in android to show that permission dialog. If particular number of attempts made then it will not show permission dialog, and we will have to move user to settings it self.

This code will help you to achieve both scenarios :

CustomPermissionManager.java :

public class CustomPermissionManager {
    public static final int STORAGE_PERMISSION = 8;
    public HashMap<Integer, ArrayList<PermissionManagerUtil.OnPermissionInterface>> onPermissionInterfaces = new HashMap<>();
    public HashMap<Integer, Boolean> hasAlreadyAskedPermission = new HashMap<>();
    private MainActivity context;

    public void init(MainActivity context) {
        this.context = context;
    }

    private boolean isAskedForFirstTime(String permissionName) {
        if (!PreferenceManager.getDefaultSharedPreferences(context).getBoolean(permissionName, false)) {
            PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(permissionName, true).commit();
            return true;
        }
        return false;
    }


    public void requestStoragePermission(Activity activity, boolean showAlertForSettingsIfNeeded, PermissionManagerUtil.OnPermissionInterface onPermissionInterface) {
        if (PermissionManagerUtil.instance.checkStoragePermission()) {
            onPermissionInterface.onPermissionGranted();
            return;
        }
        boolean isAskedFirstTime = isAskedForFirstTime(Manifest.permission.READ_EXTERNAL_STORAGE);

        if (showAlertForSettingsIfNeeded && !isAskedFirstTime && !ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_EXTERNAL_STORAGE)) {
            // if user clicked on "never ask again" then popup will not show by android
            //https://stackoverflow.com/questions/33224432/android-m-anyway-to-know-if-a-user-has-chosen-never-to-show-the-grant-permissi
            showAlertDialogWithAppSettings(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
        } else {

            if (onPermissionInterfaces.get(STORAGE_PERMISSION) == null) {
                onPermissionInterfaces.put(STORAGE_PERMISSION, new ArrayList<>());
            }
            if (onPermissionInterface != null) {
                onPermissionInterfaces.get(STORAGE_PERMISSION).add(onPermissionInterface);
            }
            if (!hasAlreadyAskedPermission.containsKey(STORAGE_PERMISSION)) {
                hasAlreadyAskedPermission.put(STORAGE_PERMISSION, true);
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION);
            }
        }
    }

    private void callPermissionManagerCallBack(int requestCode, int[] grantResults) {
        if (onPermissionInterfaces.containsKey(requestCode) && onPermissionInterfaces.get(requestCode) != null) {
            for (PermissionManagerUtil.OnPermissionInterface onPermissionInterface : onPermissionInterfaces.get(requestCode)) {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    onPermissionInterface.onPermissionGranted();
                } else {
                    onPermissionInterface.onPermissionNotGranted();
                }
            }
            hasAlreadyAskedPermission.remove(requestCode);
            onPermissionInterfaces.get(requestCode).clear();
        }

    }


    private void showAlertDialogWithAppSettings(String permission) {
        showAlertDialogWithAppSettings(context, permission);
    }

    @SuppressLint("RestrictedApi")
    private void showAlertDialogWithAppSettings(Activity context, String permission) {

        String title = "Allow permissions";
        String message = "Please allow this permission to enable this feature.";
        switch (permission) {
            case Manifest.permission.WRITE_EXTERNAL_STORAGE:
                title = "Allow Storage Permission";
                message = "Please allow permission to do.... task";
                break;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setTitle(title);

        builder.setMessage(message);
        builder.setPositiveButton("Go to settings", (dialog, which) -> {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Uri uri = Uri.fromParts("package", context.getPackageName(), null);
            intent.setData(uri);
            context.startActivity(intent);
        });
        builder.setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss());
        builder.show();
    }


    public boolean checkStoragePermission() {
        int resultExternalStorage = PermissionChecker.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return resultExternalStorage == PackageManager.PERMISSION_GRANTED;
    }
}

Call it like this :

    CustomPermissionManager customPermissionManager = new CustomPermissionManager();
    customPermissionManager.init(context);
    customPermissionManager.requestCameraPermission(true, new OnPermissionInterface() {
        @Override
        public void onPermissionGranted() {

            //permission granted
    viewOrDownloadPDF();

        }

        @Override
        public void onPermissionNotGranted() {

            // permission not granted
        }


    });

// To check permission is given or not
        boolean isGranted =  customPermissionManager.checkStoragePermission();

You can add other permission too in future like here STORAGE_PERMISSION is added, by adding same type of method.




回答3:


Put this function in common file and call whenever you use it will again check

public static boolean checkPermission(final Activity context)
    {
        int currentAPIVersion = Build.VERSION.SDK_INT;
        if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
        {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED||ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { //||ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED
                if (ActivityCompat.shouldShowRequestPermissionRationale(context, Manifest.permission.READ_EXTERNAL_STORAGE)||ActivityCompat.shouldShowRequestPermissionRationale(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)||ActivityCompat.shouldShowRequestPermissionRationale(context, Manifest.permission.CALL_PHONE)) {
                    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                    alertBuilder.setCancelable(true);
                    alertBuilder.setTitle("Permission necessary");
                    alertBuilder.setMessage("External storage permission is necessary");
                    alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                        }
                    });
                    AlertDialog alert = alertBuilder.create();
                    alert.show();
                } else {
                    ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                }
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }


来源:https://stackoverflow.com/questions/52269983/deny-permission-on-splash-screen-but-again-ask-for-allow-permission-prompt-in-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!