Paweł O Paweł O - 1 year ago 56
Android Question

Java Android requestPermissions after click button

This is how I request permissions :

private boolean checkAndRequestPermissions() {
int permissionSendMessage = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
int cameraPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
int phoneStatePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

List<String> listPermissionsNeeded = new ArrayList<>();
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
if (cameraPermission != PackageManager.PERMISSION_GRANTED) {

if (phoneStatePermission != PackageManager.PERMISSION_GRANTED) {

if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
return true;

I call this method after click button :

void loginButton() {
if(checkAndRequestPermissions()) {

But when a user confirm all permissions have to click one more time button. What I have to do that user do not click a button ?

Answer Source

You need to override the activity's onRequestPermissionsResult method, it will be called when the user accepts/rejects permissions. You can find more details HERE

Here is an example specific to your case:

public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
    if(requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS && grantResults.length == listPermissionsNeeded.size()){
        boolean allGranted = true;
        for(int grantedResult : grantedResults){
            if(grantedResult != PackageManager.PERMISSION_GRANTED){
                allGranted = false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download