Android Run Time Permission in Marshmallow

Jun 30, 2016

richa

Android

0

Introduction:

Android 6.0 Marshmellow have came up with very interesting features. One of its incredible security features is App permissions at run time, which means the Android system now offers user-facing controls over some, but not all, app permissions.

The system permissions are divided into two types, normal and dangerous.

Normal Permissions do not directly risk the user’s privacy. If app developer lists a normal permission, in app’s manifest, the system grants the permission automatically. As an example, Internet permission is a normal permission. Check this page for normal permissions.

The common thing between both permission is both permissions must be added to the android manifest like below code:

Dangerous permissions can give the app access to the user’s confidential  data. If app developer lists a dangerous permission, the user has to explicitly give approval to  an installed app.

Dangerous 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/category they accept the entire group.  An example of dangerous permission is android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE. Enabling anyone of the location permissions enables all.Following table describes both permission groups and permissions.

Permission Group Permissions
 CALENDER
  • READ_CALENDAR
  • WRITE_CALENDAR
  CAMERA
  • CAMERA
 CONTACTS
  • READ_CONTACTS
  • WRITE_CONTACTS
  • GET_ACCOUNTS
 LOCATION
  • ACCESS_FINE_LOCATION
  • ACCESS_COARSE_LOCATION
 MICROPHONE
  • RECORD_AUDIO
 PHONE
  • READ_PHONE_STATE
  • CALL_PHONE
  • READ_CALL_LOG
  • WRITE_CALL_LOG
  • ADD_VOICEMAIL
  • USE_SIP
  • PROCESS_OUTGOING_CALLS
 SENSORS
  • BODY_SENSORS
 SMS
  • SEND_SMS
  • RECEIVE_SMS
  • READ_SMS
  • RECEIVE_WAP_PUSH
  • RECEIVE_MMS        
 STORAGE
  • READ_EXTERNAL_STORAGE
  • WRITE_EXTERNAL_STORAGE

  

Now, let’s create a sample app with android studio to get clear knowledge of how these programmatically works.

Step1:  Create a new project in android studio

Open Android studio. Create New Project with com.vrired.appruntimepermissions package. You can use your preferred name too. I named my main layout as activity_main.xml and main activity as MainActivity.java.

Type the below code in activity_main.xml.

Here, I took two buttons in my layout_main.xml file. One is check_perm_btn, that is, to check whether the permission is already granted or not. Another is,request_perm_btn,used to request permission ,if permission is not granted.

STEP 2: 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.

Permissions belonging to two different groups would prompt the user with an individual dialog for each of them. If they belong to the same group, then only one dialog prompt would be displayed. The results of the requests will be passed into the method onRequestPermissionResult.

As an example, if we want to access contacts and External storage in our app,

We’ll display a prompt requesting access to these permissions when the application is launched. We will add these permissions into a string array and call the requestPermissions .

Now, we don’t want the user to keep accepting permissions that he’s already accepted. Even if the permission has been previously granted it is necessary to check again to be sure that the user did not later revoke that permission. For this the following method needs to be called on every permission.

1) checkSelfPermission (String perm);

Returns an integer value of PERMISSION_GRANTED or PERMISSION_DENIED

If a user declines a permission that is critical in the app, thenshouldShowRequestPermissionRationale(String permission); is used to describe the user the need for the permission.

Open MainActivity.java and add below code:

The runtime permission specific methods are available only since API 23. Hence the following condition is checked at each of the methods :

Now its time to check result…:-)

Run your app.and see the output.

I hope you will find this blog post helpful while working with Run Time Permission in Marshmallow in Android Application. Let me know in comment if you have any questions regarding Run Time Permission in Android Application. Please put a comment here and we will get back to you ASAP.

Got an Idea of Android App Development? Why are you still waiting for? Contact us now and see the Idea live soon.

 

Download Full Source Code FREE!!!

 

Post by richa

Leave a Reply

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