How to ask permission to make phone call from Android from Android version Marshmallow onwards?
I would better suggest to use ACTION_DIAL rather than ACTION_CALL while constructing Intent to call a particular number . Using ACTION_DIAL , you will need no call permissions in your app, as ACTION_DIAL opens the dialer with the number already entered, and further allows the user to decide whether to actually make the call or modify the phone number before calling or not call at all.
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "Your Phone_number"));// Initiates the Intent
startActivity(intent);
The stack trace seems to indicate that your permissions flow is working ok, but the call to startActivity
from onRequestPermissionsResult()
is crashing. Is the Intent
you're passing to startActivity
set correctly? I can't see it being set in that part of the code.
Note also that ContextCompat.checkSelfPermission
handles the SDK version checking on your behalf, so you should be able to use
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}
else
{
startActivity(intent);
}
by itself, without the wrapping SDK version check code.
You need to create your Intent
in onRequestPermissionsResult
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_PHONE_CALL: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+918511812660"));
startActivity(intent);
}
else
{
}
return;
}
}
}