Ask Multiple Permissions Android

Viewed 13010

I'm modifying an existing Face Tracker app Android's Facial Recognition sample projects. I'm having an issue with requesting multiple permanent permissions. The method below is a modified version of the existing method that successfully creates a pop up window to ask for camera permissions. I'm trying to replicate this with the storage permissions but so far I've been unsuccessful and I'm not sure what needs to be changed here.

 private void requestAllPermissions() {
    Log.w(TAG, "Camera + Storage permissions are not granted. Requesting permissions");

    final String[] permissions = new String[]{Manifest.permission.CAMERA};
    final String[] permissions2 = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};


    if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.CAMERA)) {
        ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
        return;
    }

    //new
    if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        ActivityCompat.requestPermissions(this, permissions2, RC_HANDLE_STORAGE_PERM);
        return;
    }


    final Activity thisActivity = this;

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityCompat.requestPermissions(thisActivity, permissions,
                    RC_HANDLE_CAMERA_PERM);
        }
    };

    View.OnClickListener listener2 = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityCompat.requestPermissions(thisActivity, permissions2,
                    RC_HANDLE_STORAGE_PERM);
        }
    };

    Snackbar.make(mGraphicOverlay, R.string.permission_camera_rationale,
            Snackbar.LENGTH_INDEFINITE)
            .setAction(R.string.ok, listener)
            .show();

    Snackbar.make(mGraphicOverlay, R.string.permission_storage_rationale,
            Snackbar.LENGTH_INDEFINITE)
            .setAction(R.string.ok, listener2)
            .show();
}
5 Answers

What you have to do is just put the permission sin a String array like

int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
        android.Manifest.permission.READ_CONTACTS,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_FINE_LOCATION
};

and to Check the permission use then

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

Now the Last thing to do finally is use it in oncreate() or onStart()

if(!hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }

I faced the issue today and answer from Aman Grover actually worked for me, you simple need to do following

final String[] permissions = new String[]{ 
    android.Manifest.permission.READ_CONTACTS, 
    Manifest.permission.READ_EXTERNAL_STORAGE
};

ActivityCompat.requestPermissions(this, permissions, 123);

This is the simplest solution:

Add this code in your MainActivity's onCreate method

//Requesting permissions
        int PERMISSION_ALL = 1;
        String[] PERMISSIONS = {
                android.Manifest.permission.READ_CONTACTS,
                android.Manifest.permission.RECORD_AUDIO,
                android.Manifest.permission.CALL_PHONE,
        };

        if (!hasPermissions(this, PERMISSIONS)) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }

Now, add this function in your main activity class

public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

You can add ass many permissions you need to ask at the application startup.

Related