FORUMS
Remove All Ads from XDA

[Tutorial] Manage System Permissions in Android Marshmallow

907 posts
Thanks Meter: 654
 
Post Reply Email Thread
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
The Following 17 Users Say Thank You to sylsau For This Useful Post: [ View ]
 
 
10th September 2016, 08:51 AM |#2  
Black_Eyes's Avatar
Senior Member
Flag Lahore
Thanks Meter: 952
 
More
Nice Tutorial.
Keep them coming
11th September 2016, 07:51 AM |#3  
OP Recognized Developer
Thanks Meter: 654
 
Donate to Me
More
Quote:
Originally Posted by Black_Eyes

Nice Tutorial.
Keep them coming

Thanks
The Following User Says Thank You to sylsau For This Useful Post: [ View ]
12th September 2016, 08:04 AM |#4  
OP Recognized Developer
Thanks Meter: 654
 
Donate to Me
More
Hello,

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

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

Thanks.

Sylvain
The Following User Says Thank You to sylsau For This Useful Post: [ View ]
29th September 2016, 09:55 PM |#5  
Jay Rock's Avatar
Senior Member
Flag Chula Vista
Thanks Meter: 319
 
More
They were intimidating at first but once you do it once, you've pretty much got the hang of it.
30th September 2016, 06:02 AM |#6  
OP Recognized Developer
Thanks Meter: 654
 
Donate to Me
More
Quote:
Originally Posted by Jay Rock

They were intimidating at first but once you do it once, you've pretty much got the hang of it.

True. When you understand the mechanism, it becomes simple to use permissions in your Android code.
1st October 2016, 07:21 AM |#7  
OP Recognized Developer
Thanks Meter: 654
 
Donate to Me
More
Quote:
Originally Posted by Christie37

As I'm new mobile developer this tutorial helped me lot. Thanks keep going!!

Great . I made some other tutorials that could be interesting for you. Don't hesitate to look at them
1st October 2016, 11:53 AM |#8  
GNVM's Avatar
Member
Thanks Meter: 23
 
More
Thanks man awsome
7th October 2016, 11:59 AM |#9  
@DSttr's Avatar
Member
Flag Tasikmalaya
Thanks Meter: 34
 
More
nice bro
7th October 2016, 12:23 PM |#10  
OP Recognized Developer
Thanks Meter: 654
 
Donate to Me
More
Quote:
Originally Posted by @DSttr

nice bro

Thanks
4th December 2016, 10:28 AM |#11  
GokulNC's Avatar
Senior Member
Flag Chennai
Thanks Meter: 1,028
 
Donate to Me
More
@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();
}
Post Reply Subscribe to Thread

Tags
android, marshmallow, permissions, system, tutorial

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes