Tuesday, 17 May 2016

Multiple runtime permission access in marshmallow version


Change your Minimum sdk version - 23

/** * RUN TIME PERMISSIONS FOR MARSHMALLOW VERSION */
 private int hasExtStoragePermission;
 private int hasReadPhoneStatePermission;
 private List<String> permissions = new ArrayList<String>();
 final static int REQUEST_CODE_SOME_FEATURES_PERMISSIONS=100;

hasExtStoragePermission = checkSelfPermission( Manifest.permission.WRITE_EXTERNAL_STORAGE );
hasReadPhoneStatePermission = checkSelfPermission( Manifest.permission.READ_PHONE_STATE );

if( hasExtStoragePermission != PackageManager.PERMISSION_GRANTED ) {
    permissions.add( Manifest.permission.WRITE_EXTERNAL_STORAGE );
}
if( hasReadPhoneStatePermission != PackageManager.PERMISSION_GRANTED ) {
    permissions.add( Manifest.permission.READ_PHONE_STATE );
}

if( !permissions.isEmpty() ) {
    requestPermissions( permissions.toArray( new String[permissions.size()] ), REQUEST_CODE_SOME_FEATURES_PERMISSIONS );
}



Then override this method in your activity where you want to show run time permission to user- 

@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch ( requestCode ) {
        case REQUEST_CODE_SOME_FEATURES_PERMISSIONS: {
            for( int i = 0; i < permissions.length; i++ ) {
                if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
                    Log.d( "Permissions", "Permission Granted: " + permissions[i] );
                } else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
                    Log.d( "Permissions", "Permission Denied: " + permissions[i] );
                }
            }
        }
        break;
        default: {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

Explaination-

step -1
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if(checkPermissions()){
        doSomething();
    } else {
        requestPermissions();
    }

} else {
    doSomething();
}
 
Step-2

private boolean checkPermissions() {
    hasExtStoragePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE );
    if( hasExtStoragePermission == PackageManager.PERMISSION_GRANTED ) {
        return true;
    }
    //If permission is not granted returning false    return false;
}


Step-3 

@RequiresApi(api = Build.VERSION_CODES.M)
private void requestPermissions() {
    if( hasExtStoragePermission != PackageManager.PERMISSION_GRANTED ) {
        permissions.add( Manifest.permission.WRITE_EXTERNAL_STORAGE );
    }
    if( !permissions.isEmpty() ) {
        requestPermissions( permissions.toArray( new String[permissions.size()] ), REQUEST_CODE_SOME_FEATURES_PERMISSIONS );
    }
}

Step-4 

@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch ( requestCode ) {
        case REQUEST_CODE_SOME_FEATURES_PERMISSIONS: {
            for( int i = 0; i < permissions.length; i++ ) {
                if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
                    doSomething();
                } else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
                    Log.d( "Permissions", "Permission Denied: " + permissions[i] );
                }
            }
        }
        break;
        default: {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

No comments:

Post a Comment