Explain Codes LogoExplain Codes Logo

Android 6.0 multiple permissions

android
permissions
best-practices
user-experience
Nikita BarsukovbyNikita Barsukov·Nov 27, 2024
TLDR

Easily handle multiple permissions in Android Marshmallow (API 23) by effectively utilizing ActivityCompat.requestPermissions. Create an array of permissions, verify each one using ContextCompat.checkSelfPermission, and request those which are not yet granted. In onRequestPermissionsResult, confirm that all the required permissions have been granted. Here is a crisp Java example for easy reference:

// A random constant for making conversations with Android more fun! public static final int REQUEST_CODE = 420; public void checkAndRequestPermissions() { String[] permissionsNeeded = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA //Say cheese! }; List<String> permissionsToRequest = new ArrayList<>(); for (String permission : permissionsNeeded) { // Inquiring if our app can party with these permissions if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { permissionsToRequest.add(permission); } } if (!permissionsToRequest.isEmpty()) { ActivityCompat.requestPermissions(this, permissionsToRequest.toArray(new String[0]), REQUEST_CODE); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_CODE) { if (allPermissionsGranted(grantResults)) { // Door's open, party time! } else { // Oops! One or more permissions opted for social distancing } } } private boolean allPermissionsGranted(int[] grantResults) { // Checking if all permissions are in party mood for (int grantResult : grantResults) { if (grantResult != PackageManager.PERMISSION_GRANTED) { return false; } } return true; }

Use this code to streamline permission requests, REQUEST_CODE can be adapted to your unique identifier to manage permissions effectively.

Implement permissions: best practices

To ensure clear communication with the user when demanded for permissions, follow below practices:

  • Educate the user: Use shouldShowRequestPermissionRationale() to explain why your app needs certain permissions.
  • Handle denials thoughtfully: Guide the user on how they can grant permissions in app settings if they were initially denied.
  • Declare permissions: Mention specific permissions in the manifest.permission section of your Android Manifest.
  • Limit requests: Prevent user concerns by asking for only necessary permissions.
  • Request at once: Combine multiple permissions into a single request to minimize disruption.
  • Differentiate response handling: Differentiate the logic for each permission in onRequestPermissionsResult() for precise control.

Dealing with the fine print

Handle permissions meticulously to ensure smoother user experience:

  • Interval between requests: Avoid flooding the user with requests. Time your requests to intercept relevant user actions.
  • Good explanations: The idea of permissions can be daunting. Use clear language in your dialogs or snackbars to explain each request.
  • Clarity of purpose: Clearly specify why each permission is needed highlighting the benefit in that feature.
  • Additional information: Some advanced users may appreciate deeper insights or reference to further details on the permission requests.

Walking the fine line

Handling permissions correctly is a blend of code flexibility and user respect:

  • Upon granting: If permissions are granted, fluidly carry on to the next action in your application.
  • Clean code: Organized and well-documented permission-related code can ease future debugging and maintenance work.
  • On denial: If permissions are denied, degrade app functionality gracefully and inform the user about the limitations.