Request individual permission on Android 6+ M

1

I am implementing the permission check at runtime for Android 6+/ Android M more or less understand the process:

  • checkSelfPermission To check the permission:
  • shouldShowRequestPermissionRationale To know if the user allows to display the request dialog.
  • requestPermissions To display the request dialog.
  • onRequestPermissionsResult Receiving the confirmation

But to make it compatible in lower versions of Android M, how is it structured, the action to be performed where it should be put in onRequestPermissionsResult or in checkSelfPermission ?

Also taking into account if the permit is required for different actions.

    
asked by Webserveis 12.09.2016 в 19:34
source

3 answers

3

You can ask for all the permissions when they open the app for the first time or when you need it, I for example have something like that to check if the permission is active

public boolean checkAudioPermission(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

now if it is not active to ask to activate it I do it like this:

ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.RECORD_AUDIO},
            MY_PERMISSIONS_REQUEST_RECORD_AUDIO);

and to receive the callback of the activation I have it like this:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_RECORD_AUDIO: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                //Aquí lo que se hace si aceptan el permiso

            } else {

                //Aquí lo que se hace si no lo aceptan
            }
            return;
        }
    }
}

Now where to add this? well I do it in the activity, or at least it is easier for me because I have a Base Activity from which the others inherit.

    
answered by 12.09.2016 / 19:55
source
1

You can use the Build class, validating if the version of your OS is > = Android 6.0 ( M ):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    //Verifica permisos para Android 6.0+
}

In this case you only review permissions for Android 6.0 or higher, for previous versions the permissions are defined in AndroidManifest.xml

Here is something important, what would your targetSDK be? If you define in your build.gradle API 23 or higher you should consider requesting permissions as it is done with Android 6.0+ if they are in the list

link

An example of how to request permissions is in this answer:

link

    
answered by 13.09.2016 в 00:43
0

From the answers and resources that I found by SO

I created the following class for resource request handling Gist: permissionutil.java v: alpha

Its use is very easy:

With hasPermissions you can check if the permission is allowed or denied With requestPermission Create copies for your customization the dialog is shown but before showing a snackbar.

if (PermissionUtil.hasPermissions(MainActivity.this,Manifest.permission.READ_CONTACTS)) {
    Log.i(TAG, "Action for: Manifest.permission.READ_CONTACTS ");
} else {
    PermissionUtil.requestPermission(MainActivity.this,parentLayout,Manifest.permission.READ_CONTACTS, REQUEST_READ_CONTACT);
}

To compute the result of the permission in onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    switch (requestCode) {
        case REQUEST_READ_CONTACT: {

            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "REQUEST_READ_CONTACT Permission has been granted by user");
            } else if (PermissionUtil.shouldWeAskPermission(MainActivity.this,permissions[0])) {
                Log.w(TAG, "REQUEST_READ_CONTACT Permission has been denied by user");
            } else {
                Log.e(TAG, "Need user go to Settings device ");
            }
            return;
        }
        default: {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
} 

And if any function gives us the following error

Some functions like getLastLocation of the GooglePlayServices give the following error:

  

Call requires permission which may be rejected by user: code should explicitly chreck to see if permission is available (with 'checkPermission') or explicitly handle a potential 'SecurityException'

Enter it with @SuppressWarnings({"MissingPermission"}) if it has already been verified first with hasPermissions

UPDATE

Using the PermissionHelper library makes it easier to apply for permissions

PermissionHelper permissionHelper;
...
permissionHelper = new PermissionHelper(this, new String[]{
        Manifest.permission.WRITE_EXTERNAL_STORAGE},
        PERM_WRITE_EXTERNAL_STORAGE);

permissionHelper.request(new PermissionHelper.PermissionCallback() {
    @Override
    public void onPermissionGranted() {
        Log.d(TAG, "onPermissionGranted() called");
    }

    @Override
    public void onPermissionDenied() {
        Log.d(TAG, "onPermissionDenied() called");
    }

    @Override
    public void onPermissionDeniedBySystem() {
        Log.d(TAG, "onPermissionDeniedBySystem() called");
    }
});

and the onRequestPermissionsResult method

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (permissionHelper != null) {
        permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
    
answered by 13.09.2016 в 16:11