How to make a phone call using intent in Android?
You can use Intent.ACTION_DIAL
instead of Intent.ACTION_CALL
. This shows the dialer with the number already entered, but allows the user to decide whether to actually make the call or not. ACTION_DIAL
does not require the CALL_PHONE
permission.
This demo will helpful for you...
On call button click:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);
Permission in Manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
EDIT IN 2021
You should write it in your manifest file but at the same time you should ask in runtime.Like this code:
if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CALL_PHONE),
REQUEST_CODE)
} else {
// else block means user has already accepted.And make your phone call here.
}
And if you want you can override onRequestPermissionsResult to give user better experience if you write same code with else block here your user will not need to click on your button again after you give permission it will directly call.
More elegant option:
String phone = "+34666777888";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(intent);
Use the action ACTION_DIAL in your intent, this way you won't need any permission. The reason you need the permission with ACTION_CALL is to make a phone call without any action from the user.
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0987654321"));
startActivity(intent);