Keywords: android | permissions | runtime | checkSelfPermission | requestPermissions
Abstract: This technical article provides an in-depth analysis of runtime permissions in Android 6.0 Marshmallow and later versions. It covers the core methods for checking and requesting permissions, including checkSelfPermission, requestPermissions, and onRequestPermissionsResult, with detailed code examples and best practices.
Introduction to Runtime Permissions
With the introduction of Android 6.0 Marshmallow, the permission model in Android shifted to a runtime-based system where users can grant or deny permissions when the app requests them. This enhances user privacy and control over their data.
Checking Permissions with checkSelfPermission
To check if a permission is granted, use the checkSelfPermission method. This method returns an integer value that can be compared to PackageManager.PERMISSION_GRANTED.
if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted, request it
}Requesting Permissions with requestPermissions
If the permission is not granted, call requestPermissions to prompt the user. This method takes an array of permissions and a request code.
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);Handling Permission Results
Override the onRequestPermissionsResult method to handle the user's response. Check the request code and grant results to proceed accordingly.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, perform the task
} else {
// Permission denied, disable functionality
}
break;
}
// Handle other permissions
}
}Handling Multiple Permissions
For apps that require multiple permissions, you can check and request them collectively. Use a list to accumulate permissions that need to be requested.
private boolean checkAndRequestPermissions() {
List<String> listPermissionsNeeded = new ArrayList<>();
// Check each permission and add to list if not granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.CAMERA);
}
// Add other permissions similarly
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[0]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}Best Practices
Always check permissions before accessing sensitive features. Provide context or explanations for why the permission is needed using shouldShowRequestPermissionRationale if the user has previously denied. Handle permission denials gracefully by disabling features or showing alternative options.
Conclusion
Implementing runtime permissions correctly is essential for Android apps targeting API level 23 and above. By using checkSelfPermission, requestPermissions, and onRequestPermissionsResult, developers can ensure a smooth user experience while respecting privacy.