androidandroid-permissionsandroid-6.0-marshmallow

How to check Grants Permissions at Run-Time?


In Android M (Preview) the user can choose a specific app and retreive specific permission.

So I am asking How to check Grants Permissions at Run-Time?


Solution

  • You can use the following code copied from https://android.googlesource.com/platform/frameworks/base/+blame/71c2c37554ae53dffdf8e210f484d92af30620fa/docs/html/preview/features/runtime-permissions.jd?pli=1#548

    Request permissions if necessary

    
    if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);
        
            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant
        
            return;
        }
    

    Handle the permissions request response

    @Override public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted, yay! do the
                    // calendar task you need to do.
    
                } else {
    
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
    
            // other 'switch' lines to check for other
            // permissions this app might request
        } }