Execution time permissions

1

I have the following code

 if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_CONTACTS,Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_CONTACTS);
            }
        }

in which question if you have permission to access the contacts and to read the memory, here everything is fine, the problem comes when rejecting the permission.

If I reject it and return for another activity, when I go back to it I should ask again if I accept or not the permission but it does not,

  

Is not this supposed to verify if you have permission and if you do not have it?   ask again?

    
asked by Bruno Sosa Fast Tag 07.11.2017 в 19:12
source

2 answers

2

Bruno, in fact, the code that you sample, check if you have defined the permission, if not, show the dialogue to request them.

Here are the definitions of the methods you use:

  

shouldShowRequestPermissionRationale () : This method shows true if the app requests permission beforehand and the user rejects the request.    Note: If the user rejects the permission request in the past and selects the Do not ask again option in the system permission request dialog, the method displays false. It also shows false if a device policy prohibits the app from having that permission

     

requestPermissions () To display the request dialog.

     

checkSelfPermission () To review the permission.

in fact the method you use does not require it again, you must dispense with shouldShowRequestPermissionRationale() since if you reject the request the dialog is not shown again.

To request them again, simply check if you have them, if not, call the requestPermissions() method:

if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

        //* Si no estan asignados los permisos, vuelve a mostrar el dialogo para requerirlos.

        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_CONTACTS,Manifest.permission.READ_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

}

This would ensure that each time you call your code to require permissions, if it does not, the dialog would be displayed.

    
answered by 08.11.2017 / 19:21
source
3

I answer you in this way because I still can not comment, anyway.

What you should do is overwrite the onRequestPermissionsResult() method and there you control your response. By this I mean that if the user tries to reject the permission you show a message or dialogue telling him it is necessary or crucial for the app to accept these permissions.

I do not know how important they are to you, I had a similar problem where I practically forced the user to accept my Location permission, if the user accepted everything was fine, if he rejected it he showed a message mentioning its importance, and if I rejected it again, I have it accept the permissions manually from the 'Settings' of Android.

I leave you a reference link that guided me a little along the way. Any questions you can ask. Greetings!

    
answered by 07.11.2017 в 19:51