How to execute request again after having accepted permission on android?

0

I have a button that when pressed it should open the phone call mode with a dynamic number.

This method requires the CALL_PHONE permission. And when it is pressed the first time, it opens a dialog asking if I authorize the requested permission. And even if the person accepted, he does not continue the call. And I can not execute the number again, because it is a dynamic number and does not have the application to find out the number clicked.

I put a function that calls the number like this: makeCall("999999 numero dinâmico aqui");

Follow my code to better understand:

   public void makeCall(String s) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + s));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            requestForCallPermission();
        } else {
            startActivity(intent);
        }
    }

    public void requestForCallPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Aqui eu preciso arranjar um modo de executar o makeCall novamente, mas não tenho como saber qual será o numero.
                }
                break;
        }
    }

How can I pass some variable through the onRequestPermissionsResult so that the application knows which number to call?

    
asked by anonymous 12.03.2018 / 09:35

2 answers

1
    private String _number;

    public void makeCall(String s) {
        _number = s;
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + s));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            requestForCallPermission();
        } else {
            startActivity(intent);
        }
    }

    public void requestForCallPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Aqui eu preciso arranjar um modo de executar o makeCall novamente, mas não tenho como saber qual será o numero.
                    makeCall(_number);
                }
                break;
        }
    }
    
12.03.2018 / 11:27
0
boolean isAutorizado(){
    return ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED;
}
private void doCall(){
    if (isAutorizado()){
        //fazer chamada
        makeCall(editextDigitado.getText().toString());
    }else{
        //pedir permissão
        requestForCallPermission();
    }
}

/**
 * Metodo que faz chamada
 * @param s
 */
public void makeCall(String s) {
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:" + s));
    startActivity(intent);        
}

public void requestForCallPermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            for (int i=0;i<permissions.length;i++) {
                String permissao = permissions[i];
                if (permissao.equals(Manifest.permission.CALL_PHONE)&& grantResults[i]==PackageManager.PERMISSION_GRANTED){
                    makeCall(editextDigitado.getText().toString());
                }
            }
            break;
    }
}
    
12.03.2018 / 17:04