Android marshmallow request permission?

后端 未结 24 2065
無奈伤痛
無奈伤痛 2020-11-21 06:40

I am currently working on an application that requires several \"dangerous\" permissions. So I tried adding \"ask for permission\" as required in Android Marshmallow(API Lev

24条回答
  •  广开言路
    2020-11-21 07:28

    I'm using this as a base Fragment class. I only ask for permissions from a fragment, but you could refactor it and make a similar Activity version.

    public class BaseFragment extends Fragment {
    
        private static final int PERMISSION_REQUEST_BLOCK_INTERNAL = 555;
        private static final String PERMISSION_SHARED_PREFERENCES = "permissions";
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode == PERMISSION_REQUEST_BLOCK_INTERNAL) {
                boolean allPermissionsGranted = true;
    
                for (int iGranting : grantResults) {
                    if (iGranting != PermissionChecker.PERMISSION_GRANTED) {
                        allPermissionsGranted = false;
                        break;
                    }
                }
    
                if (allPermissionsGranted && permissionBlock != null) {
                    permissionBlock.run();
                }
    
                permissionBlock = null;
            }
        }
    
        public void runNowOrAskForPermissionsFirst(String permission, Runnable block) {
            if (hasPermission(permission)) {
                block.run();
            } else if (!hasPermissionOrWillAsk(permission)) {
                permissionBlock = block;
                askForPermission(permission, PERMISSION_REQUEST_BLOCK_INTERNAL);
            }
        }
    
        public boolean hasPermissionOrWillAsk(String permission) {
            boolean hasPermission = hasPermission(permission);
            boolean hasAsked = hasPreviouslyAskedForPermission(permission);
            boolean shouldExplain = shouldShowRequestPermissionRationale(permission);
    
            return hasPermission || (hasAsked && !shouldExplain);
        }
    
        private boolean hasPermission(String permission) {
            return (ContextCompat.checkSelfPermission(getContext(), permission) == PackageManager.PERMISSION_GRANTED);
        }
    
        private boolean hasPreviouslyAskedForPermission(String permission) {
            SharedPreferences prefs = getContext().getSharedPreferences(PERMISSION_SHARED_PREFERENCES, Context.MODE_PRIVATE);
            return prefs.getBoolean(permission, false);
        }
    
        private void askForPermission(String permission, int requestCode) {
            SharedPreferences.Editor editor = getContext().getSharedPreferences(PERMISSION_SHARED_PREFERENCES, Context.MODE_PRIVATE).edit();
    
            editor.putBoolean(permission, true);
            editor.apply();
    
            requestPermissions(new String[] { permission }, requestCode);
        }
    }
    

    There are two key methods you should use:

    • hasPermissionOrWillAsk - Use this to see if a permission has been asked for and denies by a user who doesn't want to be asked again. This is useful for disabling UI when the user has given their final answer about NOT wanting a feature.

    • runNowOrAskForPermissionsFirst - Use this to run some code that requires permissions. If the user has already granted permission, the code will run immediately. Otherwise, the code will run later if the user grants permission. Or not at all. It's nice because you specify the code in one place.

    Here's an example:

    mFragment.runNowOrAskForPermissionsFirst(Manifest.permission.ACCESS_FINE_LOCATION, new Runnable() {
        @Override
        public void run() {
            ...do something if we have permission...
        }
    });
    

    Happy to get feedback on this. Not that this specific example is a little simplified in that you also need to check to see if Location Services are enabled on the device. (That's different than permissions.) Also, it only supports one permission at a time, but would be simple to modify if you need it to support more than one at a time.

提交回复
热议问题