[Tutorial] Manage System Permissions in Android Marshmallow

Search This thread

sylsau

Recognized Developer
Nov 8, 2011
976
672
Hello,

I create that thread to offer you a new tutorial aiming to learn how to manage System Permissions in Android Marshmallow. You can discover the tutorial in video also :


Manage System Permissions on Android 6 Marshmallow

Beginning in Android 6 Marshmallow, users grand permissions to apps while the app is running, not when they install the app. This approach gives the user more control over the app's functionality. Thus, he can choose to give the access to read contacts but not to the device location. Furthermore, users can revoke the permissions at any time, by going to the app's Settings screen.

Note that system permissions are divided into two categories : normal and dangerous. Normal permissions are granted automatically. For dangerous permissions, the user has to give approval to your application at runtime. So, developers must manage permissions at runtime before using some dangerous features.

To manage permissions inside an application, we're going to imagine we want to read contacts. This feature will use the READ_CONTACTS permission that is marked as dangerous. So, the first step is to check for READ_CONTACTS permission. If the permission has already been granted, you can use the feature that read contacts. If not, you have to request for permissions with a custom request code that will be named MY_PERMISSIONS_REQUEST_READ_CONTACTS in our example.

Code:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
      != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this,
       new String[] { Manifest.permission.READ_CONTACTS },
       MY_PERMISSIONS_REQUEST_READ_CONTACTS);

} else {
    readContacts();
}

Note that when your application requests for permissions, the system shows a standard dialog box to user that cannot be customized. Now, you need to handle the permissions request response by overriding the onRequestPermissionsResult method :

Code:
@Override
public void onRequestPermissionsResult(int requestCode,
         @NonNull String[] permissions, @NonNull int[] grantResults) {

  switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_READ_CONTACTS :
      if (grantResults.length > 0
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

         readContacts();

      } else {

         if(ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_CONTACTS)) {
            new AlertDialog.Builder(this).
                setTitle("Read Contacts permission").
                setMessage("You need to grant read contacts permission to use read" +
                           " contacts feature. Retry and grant it !").show();
         } else {
            new AlertDialog.Builder(this).
                setTitle("Read Contacts permission denied").
                setMessage("You denied read contacts permission." +
                           " So, the feature will be disabled. To enable it" +
                           ", go on settings and " +
                           "grant read contacts for the application").show();
        }

     }

     break;
  }
}

Like you can see, managing permissions in your Android application is not really hard.

Don't hesitate to give me your feedbacks or ideas for new tutorials.

Thanks.

Sylvain
 

GokulNC

Senior Member
Jan 10, 2015
826
1,440
Chennai
github.com
@sylsau Thanks..
BTW, does this work properly on Android 5.1.1 and below?

I mean, what does this below code do for 5.1.1 and below? :
Code:
ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
Does it always return true, or is there a chance of exceptions or errors?

I'm asking this because I don't have Android Device below 5.1 to test it..

Or should I wrap it up like this:
Code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    requestPermissions();
}
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 17
    Hello,

    I create that thread to offer you a new tutorial aiming to learn how to manage System Permissions in Android Marshmallow. You can discover the tutorial in video also :


    Manage System Permissions on Android 6 Marshmallow

    Beginning in Android 6 Marshmallow, users grand permissions to apps while the app is running, not when they install the app. This approach gives the user more control over the app's functionality. Thus, he can choose to give the access to read contacts but not to the device location. Furthermore, users can revoke the permissions at any time, by going to the app's Settings screen.

    Note that system permissions are divided into two categories : normal and dangerous. Normal permissions are granted automatically. For dangerous permissions, the user has to give approval to your application at runtime. So, developers must manage permissions at runtime before using some dangerous features.

    To manage permissions inside an application, we're going to imagine we want to read contacts. This feature will use the READ_CONTACTS permission that is marked as dangerous. So, the first step is to check for READ_CONTACTS permission. If the permission has already been granted, you can use the feature that read contacts. If not, you have to request for permissions with a custom request code that will be named MY_PERMISSIONS_REQUEST_READ_CONTACTS in our example.

    Code:
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
          != PackageManager.PERMISSION_GRANTED) {
    
        ActivityCompat.requestPermissions(this,
           new String[] { Manifest.permission.READ_CONTACTS },
           MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    
    } else {
        readContacts();
    }

    Note that when your application requests for permissions, the system shows a standard dialog box to user that cannot be customized. Now, you need to handle the permissions request response by overriding the onRequestPermissionsResult method :

    Code:
    @Override
    public void onRequestPermissionsResult(int requestCode,
             @NonNull String[] permissions, @NonNull int[] grantResults) {
    
      switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS :
          if (grantResults.length > 0
             && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
             readContacts();
    
          } else {
    
             if(ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {
                new AlertDialog.Builder(this).
                    setTitle("Read Contacts permission").
                    setMessage("You need to grant read contacts permission to use read" +
                               " contacts feature. Retry and grant it !").show();
             } else {
                new AlertDialog.Builder(this).
                    setTitle("Read Contacts permission denied").
                    setMessage("You denied read contacts permission." +
                               " So, the feature will be disabled. To enable it" +
                               ", go on settings and " +
                               "grant read contacts for the application").show();
            }
    
         }
    
         break;
      }
    }

    Like you can see, managing permissions in your Android application is not really hard.

    Don't hesitate to give me your feedbacks or ideas for new tutorials.

    Thanks.

    Sylvain
    1
    Hello,

    The tutorial is now also available on my blog : http://www.ssaurel.com/blog/manage-permissions-on-android-6-marshmallow/ .

    Don't hesitate to give me your advice and ideas for future tutorials.

    Thanks.

    Sylvain