Android Permission Request Code Issue

10,866

Solution 1

For lower versions you need to declare permission in manifest only, but for marshmellow you need to give it in the code, where you want to execute the code.

Here, you want to make a call. So, insert/include the code provided below in the code block written to make the call.

   public void makeCall(){
       Intent intent = new Intent(Intent.ACTION_CALL);
       intent.setData(Uri.parse("tel:" + "123456"));
       int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
       if (result == PackageManager.PERMISSION_GRANTED){
           startActivity(intent);
       } else {
           requestForCallPermission(); 
       }
  }

  private void requestPermission(){
    if(ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CALL_PHONE)){} 
    else {
           ActivityCompat.requestPermissions(activity,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) { 
                makeCall(); 
            } 
            break;
      }
  }

Solution 2

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) {
                makeCall("100");
            }
            break;
    }
}

//Now call the method makeCall("your_desire_phone_numder"); makeCall("100"); Link for more details

Share:
10,866
ueen
Author by

ueen

Updated on June 28, 2022

Comments

  • ueen
    ueen almost 2 years

    How to request a Permission? I tried to documentation, but the constant int request code MY_PERMISSIONS_REQUEST_CALL_PHONE donst seem to just work, anything else to bear in mind for Backwards compatibility?

    ActivityCompat.requestPermissions(getApplicationContext(),
                                new String[]{Manifest.permission.READ_CONTACTS},
                                MY_PERMISSIONS_REQUEST_CALL_PHONE);
    

    How to declare MY_PERMISSIONS_REQUEST_CALL_PHONE constant int?

  • ueen
    ueen about 8 years
    Thanks it started working i guess i put the startActivity in the if before :)
  • Sharad Chauhan
    Sharad Chauhan about 8 years
    Yeah that was the only problem. Keep Learning. :)
  • ueen
    ueen about 8 years
    I need the Permisson on startup and make the call on button click. And i NEED the permisson so denying is no option...
  • ueen
    ueen about 8 years
    This requiers API23 my app is minSDK 15
  • Drup Desai
    Drup Desai about 8 years
    yes you required compile SDK version to 23. Min 15 would work.
  • ueen
    ueen about 8 years
    Yea, i understand that, but how to delcare PERMISSION_REQUEST_CODE
  • Vickyexpert
    Vickyexpert about 8 years
    Sorry, I forgot to inform you about it, you need to declare it at top like below private static final int PERMISSION_REQUEST_CODE = 1;
  • Vickyexpert
    Vickyexpert about 8 years
    And also change permission in requestPermission() method at else part by CALL_PHONE instead of ACCESS_FINE_LOCATION