ActivityNotFoundException when calling Intent.ACTION_CALL
Asked Answered
W

4

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);
Wheal answered 26/1, 2015 at 8:1 Comment(3)
Isn't ACTION_DIAL the right one? i don't think you can start a call without any user input.Posturize
You are calling the number - so what input you need if you have it already?Abb
I am getting contact number from user input, It was just an example I used in my question. I cannot use ACTION_DIAL because I need to call without showing dial-upWheal
W
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");
Wheal answered 26/1, 2015 at 9:21 Comment(2)
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.Gonfalonier
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 }Ortiz
A
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.

Abb answered 26/1, 2015 at 8:32 Comment(1)
I don't think using try-catch I would solve my problemWheal
E
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
  }
Eldwon answered 26/1, 2015 at 8:9 Comment(4)
Which part of my answer is wrong? I did not mention anything about permission. And official documentation also recommends ACTION_DIAL over ACTION_CALL developer.android.com/reference/android/content/…Eldwon
I have already taken permission <uses-permission android:name="android.permission.CALL_PHONE" /> I cannot use ACTION_DIAL because I need to call contact directly without showing dial-up.Wheal
This problem occurred in LG Nexus-4 Version (5.0.1) not in TabletWheal
Maybe you should try setting EXTRA_PHONE_NUMBER instead? developer.android.com/reference/android/content/…Eldwon
W
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");
Wheal answered 26/1, 2015 at 9:21 Comment(2)
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.Gonfalonier
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 }Ortiz
T
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);
    }
Tact answered 20/7, 2016 at 3:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.