Intent Action Call in Android 5

2019-02-15 09:03发布

问题:

I have this code, that works fine in Android 4.4 and previous:

Intent intent = new Intent(Intent.ACTION_CALL);         
intent.setPackage("com.android.phone");
intent.setData(Uri.parse("tel:" + number));
context.startActivity(intent);

Now, in Android 5.0 Lollipop this code doesn't work, and shows this exception:

Fatal Exception: android.content.ActivityNotFoundException
No Activity found to handle Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxx pkg=com.android.phone }

In the documentation, this Intent doesn't appear deprecated:

Any idea? Thanks in advance

回答1:

Seems like the package name has been changed from

com.android.phone 

to

com.android.server.telecom.

Hope this helps!



回答2:

An alternative to using the action String manually encoded is to use the default intent like so:

Intent out = new Intent(Intent.ACTION_CALL );
out.setData(Uri.parse("tel:" + Uri.encode("+12345#123")));
startActivity(out);

This will pass the intent to the system and all apps with phone capability will respond instead of the specific one determined via the action String



回答3:

This means you are trying to call com.android.phone but it's not there. No miracles. It's not gonna work. Either the package is named differently or you are using semi-backed emulator or so with missing stuff. Not to mention you must always have try/catch around startActivity() as there's no guarantee it gonna success (especially when targeting external packages)



回答4:

This one worked for me on Android 4.4:

    Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setPackage("com.android.dialer");
    intent.setData(Uri.parse("tel:1111111111"));
    startActivity(intent); 

If using Eclipse, open the system dialer app and in DDMS, check for the name of the dialer package; in my case was "com.android.dialer".