Calling a number from dialpad returns a warning: "The call requires permission, which can be rejected by the user"

I am trying to basically dial a number when the user clicks on a TextView with a number:

number_title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:+"+user.getTelephone())); activity.startActivity(callIntent); //the above line returns the warning given below } }); 

The warning I get:

A call requires permission, which can be rejected by the user. Code to explicitly check if permission is available.

I understand that an application must request permission if this permission was not previously granted. My question is: how can I request permission explicitly before the call?

+5
source share
3 answers

Well, it's about allowing runtime, so only declaring them in the manifest will not work. Instead, you should check the right before starting the call.

Something like this should work - the user will only be asked permission if he has not provided it earlier (or if he revoked it):

 private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1234; public void yourfunction() { number_title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE); } else { executeCall(); } } }); } private void executeCall() { // start your call here activity.runOnUiThread(new Runnable() { @Override public void run() { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:+"+user.getTelephone())); activity.startActivity(callIntent); } }); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_CALL_PHONE: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! executeCall(); } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } } } 

I took the above code mainly from google: http://developer.android.com/training/permissions/requesting.html Follow the link if you want more information.

Edit: onRequestPermissionsResult will be a callback function in your activity. You may need to handle the call.

+2
source

This is just a sample reference call to check permission:

 if(!checkSelfPermission(Manifest.permission. CALL_PHONE)==PackageManager.PERMISSION_GRANTED){ requestPermissions(this,new String {}(Manifest.permission. CALL_PHONE),INT_TO_CHECK RESULT); } 

Let me know if this works.

Edited: Changed code to explain.

+2
source

use this, maybe this will work:

  <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
0
source

Source: https://habr.com/ru/post/1240988/


All Articles