How to ask multiple permissions at same time in An

2019-01-26 02:21发布

问题:

Following code having multiple permissions, My error is when ever onclick button it asking only one permission, and again i click means it asking another permission.

My Onclick

            public void onClick(View v) {
            getPermissionCamara();
            getStroagePermission();
            selectImage();
         }

My permissions are like this

                private static final int CAMERA_PERMISSIONS_REQUEST = 1;
private static final int STROAGE_PERMISSIONS_REQUEST = 2;
public void getPermissionCamara() {
    if (ContextCompat.checkSelfPermission(this, CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        if (shouldShowRequestPermissionRationale(
                CAMERA)) {


        }
        requestPermissions(new String[]{CAMERA},CAMERA_PERMISSIONS_REQUEST);
    }
}


public void getStroagePermission() {
    if (ContextCompat.checkSelfPermission(this,READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        if (shouldShowRequestPermissionRationale(
                READ_EXTERNAL_STORAGE)) {


        }
        requestPermissions(new String[]{READ_EXTERNAL_STORAGE},STROAGE_PERMISSIONS_REQUEST);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[],
                                       @NonNull int[] grantResults) {
    // Make sure it's our original READ_CONTACTS request
    if (requestCode == CAMERA_PERMISSIONS_REQUEST) if (grantResults.length == 1 &&
            grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show();
    }
    else if (requestCode == STROAGE_PERMISSIONS_REQUEST) if (grantResults.length == 2 &&
            grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
    }
    }

回答1:

requestPermissions(new String[]{
                                Manifest.permission.READ_CONTACTS,
                                Manifest.permission.ACCESS_FINE_LOCATION},
                        ASK_MULTIPLE_PERMISSION_REQUEST_CODE);


回答2:

I have done it this way:

public static final int MULTIPLE_PERMISSION_REQUEST = 43;

public boolean requestPermission(String... permissions) {
    final List<String> permissionsList = new ArrayList<>();

    for (String perm : permissions) {
        addPermission(permissionsList, perm);
    }

    if (permissionsList.size() > 0) {
        if (Utils.hasMarshmallow())
            requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                    MULTIPLE_PERMISSION_REQUEST);
        else
            ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]),
                    MULTIPLE_PERMISSION_REQUEST);
        return false;
    } else
        return true;
}

private boolean addPermission(List<String> permissionsList, String permission) {
    if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
        permissionsList.add(permission);
        // Check for Rationale Option
        if (Utils.hasMarshmallow())
            if (!shouldShowRequestPermissionRationale(permission))
                return false;
    }
    return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    /*switch (requestCode) {
        case PERMISSION_REQUEST:
        case MULTIPLE_PERMISSION_REQUEST: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // task you need to do.
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
        }
    }*/
}