Intent Action Call in Android 5

左心房为你撑大大i 提交于 2019-12-19 04:06:14

问题


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



来源:https://stackoverflow.com/questions/27102000/intent-action-call-in-android-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!