ActivityNotFoundException when calling Intent.ACTION_CALL

Nooruddin Lakhani picture Nooruddin Lakhani · Jan 26, 2015 · Viewed 14.6k times · Source

I get ActivityNotFoundException when perform Intent.ACTION_CALL operation. I found many links but all of it couldn't solve my problem.

It sometimes gives me exception like

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CALL dat=tel:xxx-xxx-xxxx pkg=com.android.phone (has extras) }

I have used the following code in my project

String contact_number="123456789";
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + contact_number));
startActivity(callIntent);

Answer

Marcin Orlowski picture Marcin Orlowski · Jan 26, 2015

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.