Working with Android Marshmallow 6.0 Permissions - Androhub

Marshmallow Permission Banner

Working with Android Marshmallow 6.0 Permissions

Android Marshmallow 6.0 final stable version is released in October 2015. Android Marshmallow brought some new API changes and one of the most important API change is the addition of new granular permissions. Below is the type of permissions required for Marshmallow Devices:

Normal and Dangerous Permissions

In Marshmallow these dangerous permissions should be granted at run time and the normal permissions are granted automatically. For Lollipop (API level 22) or lower android versions the permissions specified in manifest are granted at installation.

Due to this permissions now no more popup dialog of permissions while installing app from Play Store. The permissions will be asked manually during run time via asking user to allow or deny the permissions. Below is the type of permissions dialog:

  1. Pre – Marshmallow Devices Permissions Dialog : 

    Pre MarshMallow Permission
    Pre MarshMallow Permission
  2. Marshmallow Devices Run Time Permissions Dialog : 
Marshmallow Permission
Marshmallow Permission

 

You can navigate to App Info and go to Permission option to revoke back and grant access the permissions manually.

App Info Permission
App Info Permission

Example

In this tutorial, we are going to learn how to check if the permission is already granted and if not granted how to grant permissions.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open strings.xml located under res=>values folder and add following strings.

3.Create activity_main.xml and add the following code. In this xml file add the button to access permission.

4. Now open your AndroidManifest.xml and add the following permission for Pre-Marshmallow Devices.

5. Now to access permission for Marshmallow Devices below are some steps to do it:

  • Check if Permission is already granted : Lets check permission for ACCESS_FINE_LOCATION, if permission is granted then it will display toast else it will ask user to Allow or Deny the Location permission.

  • If Permission is not Granted Open Permission Dialog: If permission is not granted a dialog will open to ask user permission. Here we need to pass an Unique Id as we used to send in StartActivityForResult to get back result from user action.

  • OnRequestPermissionResult: Now when the user allow or deny the permission then response will be check via onRequestPermissionResult() method same as onActivityResult().

6. For Permission Intent Id’s we have created a class naming MarshmallowIntentId.java and put all Intent Id into it and access from here only.

7. Now to ask permission to user we have to create a class naming MarshmallowPermission.java and add all required permission methods into it and call it whenever we require.

8. Finally create MainActivity.java and add the following code. In this class we need to check the permission is granted or not on button click and grant permission if not granted.

9. Finally, all done – now you can also add permissions to your app on run time.

Thanks. 🙂

 

3 Comments

Jagdish
Tuesday, October 10th, 2017

Thanks Dr. Droid. Very very helpful !!!

thomas
Tuesday, August 7th, 2018

Which permission would I need to set for screenshots?

Where do I put the step:

Check if Permission is already granted : Lets check permission for ACCESS_FINE_LOCATION, if permission is granted then it will display toast else it will ask user to Allow or Deny the Location permission.

Dr. Droid
Wednesday, August 8th, 2018

Hi Thomas,

You will be needing WRITE_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions. If permission is granted then take screenshot else ask user to allow/deny.

Make sure you also put uses_permission in AndroidManifest.xml too.

Thanks

Post comment

Your email address will not be published. Required fields are marked *