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
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;
}
}