When to request permission at runtime for Android Marshmallow 6.0?

后端 未结 11 971
故里飘歌
故里飘歌 2020-12-06 05:54

I am testing my app on Marshmallow 6.0 and it\'s getting force closed for the android.permission.READ_EXTERNAL_STORAGE, even if it is defined in th

11条回答
  •  独厮守ぢ
    2020-12-06 06:35

    A good explanation and HowTo can be found here:

    https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

    I wrote this code to check and request the permissions at runtime in a BaseActivity.class which is parent of every other Activity.class I implemented:

    public static final int PERMISSION_REQUEST = 42;
    public static final int MULTIPLE_PERMISSION_REQUEST = 43;
    
    //Marshmallow Permission Model
    public boolean requestPermission(String permission /* Manifest.permission...*/) {
        if (ContextCompat.checkSelfPermission(this,
                permission) != PackageManager.PERMISSION_GRANTED) {
            if (Utils.hasMarshmallow())
                ActivityCompat.requestPermissions(this,
                        new String[]{permission}, PERMISSION_REQUEST
                );
            else {
                requestPermissions(new String[]{permission},
                        PERMISSION_REQUEST);
            }
            return false;
        } else {
            return true;
        }
    }
    
    public boolean requestPermission(String... permissions) {
        final List permissionsList = new ArrayList();
    
        for (String perm : permissions) {
            addPermission(permissionsList, perm);
        }
    
        if (permissionsList.size() > 0) {
            if (Utils.hasMarshmallow())
                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                        MULTIPLE_PERMISSION_REQUEST);
            else
                ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]),
                        MULTIPLE_PERMISSION_REQUEST);
            return false;
        } else
            return true;
    }
    
    
    private boolean addPermission(List permissionsList, String permission) {
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (Utils.hasMarshmallow())
                if (!shouldShowRequestPermissionRationale(permission))
                    return false;
        }
        return true;
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST:
            case MULTIPLE_PERMISSION_REQUEST: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
    
                } else {
    
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
    
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    

    Simply example call:

    activity.requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    

    Return result will let you know if the permission is already granted or not.

提交回复
热议问题