Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Marshmallow, how to prompt two or more permissions dialog one by one like Hangouts does?

I'm handling my application code to work in Marshmallow devices, I'm managing its permissions dialog to show in needed places.

Currently held up with this scenario where it required two permission (Location and Storage) and I want to ask one by one like how Hangout does. Couldn't find how it's customized, any solution?

enter image description here enter image description here

Here is the code I handle for single permission:

case REQUEST_CODE_WRITE_EXTERNAL_STORAGE: {

    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        /Permission is granted
        Toast.makeText(this, "SDK >= 23 & permission Granted ", Toast.LENGTH_SHORT).show();
        return true;

    } else {
        //Permission is revoked
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE);
        return false;
    }
}

And in onRequestPermissionsResult():

case REQUEST_CODE_WRITE_EXTERNAL_STORAGE: {

    // If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        // permission was granted, yay! Do the
        // contacts-related task you need to do.
        Log.e("PMS", "granted");
        Toast.makeText(this, "SDK >= 23 & permission Granted ", Toast.LENGTH_SHORT).show();

    } else {
        Log.e("PMS", "Not Granted");

        // permission denied, boo! Disable the
        // functionality that depends on this permission.
        int checkStatus = getPermissionStatus(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (checkStatus == 3) {
            Toast.makeText(this, "SDK  >= 23 & permission Denied ", Toast.LENGTH_SHORT).show();

        } else if (checkStatus == 4) {
            Toast.makeText(this, "SDK  >= 23 & permission Blocked ", Toast.LENGTH_SHORT).show();

        }
    }
    return;
}
like image 832
SaravanaRaja Avatar asked Oct 18 '22 12:10

SaravanaRaja


1 Answers

For requesting multiple permission one by one you need to add all permission in second parameter String[] ActivityCompat.requestPermissions method. Like this:

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE);

Let me know if it helps

like image 77
Sagar Trehan Avatar answered Nov 01 '22 16:11

Sagar Trehan