ActivityNotFoundException when calling Intent.ACTION_CALL
You should check if there are any activities that handle this intent before starting it. This can happen if your app run on a tablet with wifi only and has no phone capability. Also if you only intend to launch dialer, better use ACTION_DIAL
than ACTION_CALL
which makes the call directly from your app.
final Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"));
if (dialIntent.resolveActivity(context.getPackageManager()) != null) {
// put your logic to launch call app here
}
I have solved this issue. I have used the following code
String contact_number="123456789";
Intent callIntent = new Intent(Intent.ACTION_CALL);
intent.setPackage("com.android.phone");
callIntent.setData(Uri.parse("tel:" + contact_number));
startActivity(callIntent);
I have replaced this line for Lollipop
intent.setPackage("com.android.phone");
with
intent.setPackage("com.android.server.telecom");
There's no guarantee that given intent can be handled (i.e. tablets may not have telephony app at all). If there's no application with matching intent-filter
, then you will face ActivityNotFoundException
. The proper approach is to be aware of this and use try/catch
to defeat the crash and properly recover:
try {
String contact_number="123456789";
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + contact_number));
startActivity(callIntent);
} catch (Exception e) {
// no activity to handle intent. show error dialog/toast whatever
}
Also you should be rather using ACTION_DIAL
instead, as ACTION_CALL
requires additional permission.