10

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);
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39

4 Answers4

14

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
13

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
  }
hidro
  • 12,333
  • 6
  • 53
  • 53
  • Which part of my answer is wrong? I did not mention anything about permission. And official documentation also recommends ACTION_DIAL over ACTION_CALL http://developer.android.com/reference/android/content/Intent.html#ACTION_CALL – hidro Jan 26 '15 at 08:37
  • I have already taken permission I cannot use ACTION_DIAL because I need to call contact directly without showing dial-up. – Nooruddin Lakhani Jan 26 '15 at 08:49
  • This problem occurred in LG Nexus-4 Version (5.0.1) not in Tablet – Nooruddin Lakhani Jan 26 '15 at 08:50
  • Maybe you should try setting `EXTRA_PHONE_NUMBER` instead? http://developer.android.com/reference/android/content/Intent.html#EXTRA_PHONE_NUMBER – hidro Jan 26 '15 at 09:06
6

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");
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
  • sir what if i want to handover incoming call from my app to default system app? do you know anything about it? i am using this method with "ACTION_ANSWER" but not working. – Mateen Chaudhry Sep 02 '18 at 06:27
  • with your code i have got the error:No Activity found to handle Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx pkg=com.android.phone } – Vasu Nov 02 '18 at 05:49
0

this is my code.

if (BuildUtil.isAndroidM()) {
        if (ActivityCompat.checkSelfPermission(mActivity,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(mActivity, PermissionUtil.requestPermissionFineAndCoarse(),
                    PHONE_REQUEST_CODE);
        }
    } else {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        if (BuildUtil.isAndroidL()) {
            callIntent.setPackage("com.android.server.telecom");
        } else {
            callIntent.setPackage("com.android.phone");
        }
        callIntent.setData(Uri.parse("tel:" + phone));
        startActivity(callIntent);
    }
raditya gumay
  • 2,951
  • 3
  • 17
  • 24