How to add runtime permission in android apps?

I know, it is a bit late to discuss about Runtime Permissions as there are two higher version of the Android OS versions already rolled out. Here I want to share the thought how we are integrating Runtime Permissions in our all Android apps. Lets we guide you how to add runtime permission in android.

From Android OS 6.0 (API level 23), user needs to grant some of permission when app is running and app wants to access some of user’s privacy. Below API level 23 a flow remains same as what we were doing previously – no runtime permission requires.

This blog will help you to the how to implement runtime permission using few line of code. There are many user-permissions in Android but I am only going to focus on some of the most used.

Also demonstrates single permission and group of permission.

Check Self Permissions

checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)

Requesting Permissions

The method requestPermissions(String[] permissions, int requestCode); is a public method that is used to request dangerous permissions. We can ask for multiple dangerous permissions by passing a string array of permissions.

ActivityCompat.requestPermissions(this, permissions, requestCode);

Dangerous vs. Non-Dangerous Permissions

Android defines some permission as “dangerous” and some permission as “normal.” Both are required in your application’s manifest but only dangerous permissions require a runtime request. Normal permissions are accepted at install time and cannot be revoked later. An example of a normal permission is android.permission.INTERNET. Dangerous permissions are grouped into categories that make it easier for the user to understand what they are allowing the application to do. If the user accepts one permission in a group they accept the entire group. The opposite is true as well, if the user denied one permission in a group, the entire group in denied. The example application demonstrates this with both FINE_LOCATION and COARSE_LOCATION of the group LOCATION. You will notice that once the user has granted permission for one, the application is automatically granted permission for the other without the need to ask.

Know more about dangerous permissions from here

Code

AndroidManifest.xml

<uses-permission android:name=”android.permission.READ_CONTACTS” />

    <uses-permission android:name=”android.permission.CAMERA” />

    <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

PermissionSampleActivity.java

To start using this sample project you just need to extends RuntimePermissionActivity :

public class PermissionSampleActivity extends RuntimePermissionActivity {

   …

 }

Single permission

For each single permission :

 

if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {

    askRequestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, R.string.enable_permission, REQUEST_CONTACT_PERMISSIONS);

} else {

    // Permission already granted.

    // add your logic

}

Multiple permissions

For each group permission :

 

if ((ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)

     || (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)

     || (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {

     askRequestPermissions(new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},

             R.string.enable_permission, REQUEST_GROUP_PERMISSIONS);

} else {

      // Permission already granted.

      // add your logic

}

Permission Granted

After granting permission write down your method or custom code :

 

@Override

    public void onRequestPermissionsGranted(int requestCode) {

        switch (requestCode) {

            // Single permission granted

            case REQUEST_CONTACT_PERMISSIONS:

                //TODO Add your logic

                break;

           

            // Group permission granted

            case REQUEST_GROUP_PERMISSIONS:

                //TODO Add your logic

                break;

        }

    }