Multiple Runtime Permissions From Same Permission Groups

suggest change

In the manifest we have four dangerous runtime permissions from two groups.

<!-- Required to read and write external storage. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<!-- Required to get location of device. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

In the activity where the permissions are required. Note it is important to check for permissions in any activity that requires permissions, as the permissions can be revoked while the app is in the background and the app will then crash.

final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_layout);
    
    // A simple check of whether runtime permissions need to be managed 
    if (Build.VERSION.SDK_INT >= 23) {
        checkMultiplePermissions();
    }

On the Android 6.0 permission model this page uses, granting one dangerous permission in a group also grants the other manifest permissions from that group. permissionsNeeded is filled and never read, so this method requests every entry in permissionsList.

private void checkMultiplePermissions() {

 if (Build.VERSION.SDK_INT >= 23) {
     List<String> permissionsNeeded = new ArrayList<String>();
     List<String> permissionsList = new ArrayList<String>();
     
     if (!addPermission(permissionsList, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
         permissionsNeeded.add("GPS");
     }

     if (!addPermission(permissionsList, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
         permissionsNeeded.add("Read Storage");
     }
     
     if (permissionsList.size() > 0) {
         requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                 REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
         return;
     }
 }
}
private boolean addPermission(List<String> permissionsList, String permission) {
    if (Build.VERSION.SDK_INT >= 23)

        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);

            // Check for Rationale Option
            if (!shouldShowRequestPermissionRationale(permission))
                return false;
        }
    return true;
}

This deals with the result of the user allowing or not allowing permissions. In this example, if the permissions are not allowed, the app is killed. If permissions is empty, the map stays at PERMISSION_GRANTED and the method treats the request as granted.

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
        
            Map<String, Integer> perms = new HashMap<String, Integer>();
            // Initial
            perms.put(android.Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
            perms.put(android.Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
            
            // Fill with results
            for (int i = 0; i < permissions.length; i++)
                perms.put(permissions[i], grantResults[i]);
            if (perms.get(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    && perms.get(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                // All Permissions Granted
                return;
            } else {
                // Permission Denied
                if (Build.VERSION.SDK_INT >= 23) {
                    Toast.makeText(
                            getApplicationContext(),
                            "My App cannot run without Location and Storage " +
                                    "Permissions.\nRelaunch My App or allow permissions" +
                                    " in Applications Settings",
                            Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        }
        break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

More Information https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents