Permit Application

2

I am currently asking for permission separately:

if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // no explico el porque es importante aceptar
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        READ_EXTERNAL_STORAGE);
            }
        }



if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {
                // no explico porque es importante aceptar el permiso
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_CONTACTS},
                        MY_PERMISSIONS_REQUEST_READ_CONTACTS);
            }
        }

It's impossible for me to put all this together in one method, I do not understand the syntax, could someone help me out, thank you.

    
asked by Bruno Sosa Fast Tag 10.11.2017 в 14:05
source

2 answers

1

You can verify first if both permissions are accepted by the & &, operator, if any are not, it is required, in this case you can add the permissions in the array:

new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_CONTACTS}

Code:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)) {
        Toast.makeText(this, "No tiene permisos" , Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_CONTACTS}, 0);
    }
    else
    {
        Toast.makeText(this, "tiene Permisos" , Toast.LENGTH_LONG).show();
    }

I recommend using requestPermissions , instead of shouldShowRequestPermissionRationale

    
answered by 10.11.2017 / 15:12
source
1

Try asking for multiple permissions by specifying in the string array the permissions you are requesting:

ActivityCompat.requestPermissions(this,
    new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_CONTACTS
    },
    PERMISSION_REQUEST_CODE);
    
answered by 10.11.2017 в 15:08