Intent Action Call in Android 5

2019-02-15 08:29发布

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

4条回答
走好不送
2楼-- · 2019-02-15 08:45

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楼-- · 2019-02-15 08:53

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)

查看更多
ゆ 、 Hurt°
4楼-- · 2019-02-15 09:02

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".

查看更多
Anthone
5楼-- · 2019-02-15 09:10

Seems like the package name has been changed from

com.android.phone 

to

com.android.server.telecom.

Hope this helps!

查看更多
登录 后发表回答