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

Asked

Viewed 145 times

0

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

This method requires permission CALL_PHONE. And when pressed the first time, it opens a dialog asking if I authorize the requested permission. And even if the person accepts, he doesn’t continue the connection. And I do not have to run the number again, because it is a dynamic number and there is no way the application to find out what was the number clicked.

I put a function that calls the number that way: 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 the app knows which number to call?

  • 1

    Ever thought about saving the number in a class variable?

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;
        }
    }
  • 1

    Thank you Perozzo your answer worked perfectly. That’s just what I needed.

  • 1

    @Fernandovr Magina! Happy to help!

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;
    }
}
  • This answer does not suit me Weslley, because there is no text field in Activity, it pulls the number through a database, where the request is made only once. The reply of friend Perozzo below was the most correct for me. Still thanks for responding.

  • was just put the number in the place right, but okay

Browser other questions tagged

You are not signed in. Login or sign up in order to post.