Tag Archives: new permission

New Permission Model in Android

Hi Folks, today we will be seeing the new permission model in action and how to handle it. You know that we define the system permissions that we need to run the app like Camera and “READ_EXTERNAL_STORAGE” and “WRITE_EXTERNAL_STORAGE” permissions in the manifest file. But this is not enough for the from the API level 23 , Android 6.0, the new model is introduced that is getting the permission from the user on the run time.

So we will see how we can do it. For this I am using the latest Build Tools 23.0.2
And the Compiled SDK Version is API 23

Always make sure you are using the latest build tools as Google always improve the backward compatibility through it.

Ok the the first thing you have to understand is, we will be checking this permission availability exactly before the place where we access the permission related operation. Adding this code will have no special effect on the phones that are running versions less than Android 6.0

And we have to add the regular permission as usual in the Manifest too. Else again the app will crash.

We will be doing a sample on getting a screen shot and saving it to the SD Card so here we need the “WRITE_EXTERNAL_STORAGE” permission

Step 1
At the place where we have to invoke the screenshot function we check if the permission is already available. If the permission is already available we proceed with the taking screen shot, else we call another methods which will invoke the  new permission request procedure.

if(PackageManager.PERMISSION_GRANTED== ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
    shareScreenShot();
}else{
    requestWritePermission(MainActivity.this);
}

Step 2
the method requestWritePermission(context) will check if the permission is already requested and if already requested and rejected it shows another Custom Dialog which explains why the permission is needed and when the Allow button is click it shows the Permission request again. Meanwhile if this is the first time the request permission procedure will automatically fired.
While we invoke the ‘ActivityCompat.requestPermissions’ method we pass a request code, in our case it is “REQUEST_WRITE_EXTERNAL_STORAGE” so that we can track back the result on the onRequestPermissionResult() override method and respond to it appropriately.

private static void requestWritePermission(final Context context){
    if(ActivityCompat.shouldShowRequestPermissionRationale((Activity)context,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        new AlertDialog.Builder(context)
                .setMessage("This app needs permission to write data to the phone so that the Screenshot can be saved")
                .setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE_EXTERNAL_STORAGE);
                    }
                }).show();

    }else {
        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE);
    }
}

Step 3
In the onRequestPermissionResult() we catch the reult from the permission dialog and decide whether to go for the screenshot procedure or not

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {

        case REQUEST_WRITE_EXTERNAL_STORAGE: {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                shareScreenShot();
            } else {
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
            return;
        }
    }
}

So here is the order in it work, these are taken from one of my other app, and its done for reading from the SD card
step1    Step 2   Step 3

So that’s it guys hope you guys understood and will implement in your own application
Sample app can be found at the GitHub gitHub-download-button

You can get more info from the official documentation here :  http://developer.android.com/training/permissions/requesting.html