Get requestPermissions result when requesting permissions

0

I need to read and write files of a specific route in my SDcard, when I work with Android versions that do not require permission, I do not have problems, but when I work with versions that require permissions I am writing the following:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            // mis rutinas
        }else{
            ActivityCompat.requestPermissions(PrincipalActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
            return;
        }
    }else{
        // mis rutinas;
    }

I want to know if it is possible to obtain the result after accepting or not the requested permission with ActivityCompat.requestPermissions , in order to launch a routine that requires this result.

Thank you.

    
asked by Brando T. 01.11.2016 в 22:43
source

2 answers

0

I got what I needed with the following:

final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
...
...
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        // mis rutinas
    }else{
        ActivityCompat.requestPermissions(PrincipalActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CODE_ASK_PERMISSIONS);
        return;
    }
}else{
    // mis rutinas;
}

and then to validate whether the permissions were accepted or not, I use the onRequestPermissionsResult method, leaving the following:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case REQUEST_CODE_ASK_PERMISSIONS:
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                // Permission Granted
                // Rutina que se ejecuta al aceptar
            }else{
                // Permission Denied
                Toast.makeText(PrincipalActivity.this, "No se aceptó permisos", Toast.LENGTH_SHORT).show();
            }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
    
answered by 03.11.2016 / 04:15
source
0

Have you tried, after requiring the permits, to check if they were accepted?

...
...
...
ActivityCompat.requestPermissions(PrincipalActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);        

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        // Permiso Aceptado.
    }else{
       // Permiso NO aceptado.
    }
...
...
...
    
answered by 02.11.2016 в 00:18