Android Permission Request Code Issue

2020-01-29 17:34发布

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?

4条回答
冷血范
2楼-- · 2020-01-29 17:59

Try below code hope it will help you. First this will ask you for permission popup after allowing it will call the number.

if (ContextCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(HomePanelActivity.this,
                    Manifest.permission.CALL_PHONE)) {
                ActivityCompat.requestPermissions(HomePanelActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PERMISSION);
            }
        } else {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + phoneNumber));
            if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                startActivity(callIntent);
            }
        }

@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 10:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + phoneNumberToCall));
                    if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                        startActivity(callIntent);
                    }
                } else {
                    Snackbar.make(drawerLayout, "You Deny permission", Snackbar.LENGTH_SHORT).show();
                return;
            }
        }
    };
查看更多
趁早两清
3楼-- · 2020-01-29 18:02

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.ACCESS_FINE_LOCATION},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;
      }
  }
查看更多
▲ chillily
4楼-- · 2020-01-29 18:04
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + "123456"));
            startActivity(intent);

Try doing this.

查看更多
放我归山
5楼-- · 2020-01-29 18:08
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

查看更多
登录 后发表回答