本文翻译自:How to make a phone call using intent in Android?
I'm using the following code to make a call in Android but it is giving me security exception please help. 我正在使用以下代码在Android中拨打电话,但这给我带来了安全异常,请提供帮助。
posted_by = "111-333-222-4";String uri = "tel:" + posted_by.trim() ;Intent intent = new Intent(Intent.ACTION_CALL);intent.setData(Uri.parse(uri));startActivity(intent);
permissions 权限
<uses-permission android:name="android.permission.CALL_PHONE" />
Exception 例外
11-25 14:47:01.661: ERROR/AndroidRuntime(302): Uncaught handler: thread main exiting due to uncaught exception
11-25 14:47:01.681: ERROR/AndroidRuntime(302): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:111-333-222-4 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{43d32508 302:com.Finditnear/10026} (pid=302, uid=10026) requires android.permission.CALL_PHONE
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.os.Parcel.readException(Parcel.java:1218)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.os.Parcel.readException(Parcel.java:1206)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1214)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.app.Activity.startActivityForResult(Activity.java:2749)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.app.Activity.startActivity(Activity.java:2855)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at com.Finditnear.PostDetail$2$1$1$1.onClick(PostDetail.java:604)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.widget.ListView.performItemClick(ListView.java:3285)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
#1楼
参考:https://stackoom.com/question/HwIY/如何在Android中使用Intent拨打电话
#2楼
You can use Intent.ACTION_DIAL
instead of Intent.ACTION_CALL
. 您可以使用Intent.ACTION_DIAL
代替Intent.ACTION_CALL
。 This shows the dialer with the number already entered, but allows the user to decide whether to actually make the call or not. 这将显示已输入号码的拨号程序,但允许用户决定是否实际拨打电话。 ACTION_DIAL
does not require the CALL_PHONE
permission. ACTION_DIAL
不需要CALL_PHONE
权限。
#3楼
您也可以使用此:
String uri = "tel:" + posted_by.replaceAll("[^0-9|\\+]", "");
#4楼
This demo will helpful for you... 该演示将为您提供帮助...
On call button click: 在通话按钮上,单击:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);
Permission in Manifest: 清单中的权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
#5楼
use this full code 使用完整的代码
Intent callIntent = new Intent(Intent.ACTION_DIAL);callIntent.setData(Uri.parse("tel:"+Uri.encode(PhoneNum.trim())));callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(callIntent);
#6楼
In Android for certain functionalities you need to add the permission to the Manifest file. 在Android中,对于某些功能,您需要将权限添加到清单文件中。
- Go to the Projects AndroidManifest.xml 转到项目AndroidManifest.xml
- Click on the Permissions Tab 单击权限选项卡
- Click on Add 点击添加
- Select Uses Permission 选择使用权限
- See the snapshot below 见下面的快照
6.Save the manifest file and then run your project. 6.保存清单文件,然后运行您的项目。 Your project now should run as expected. 您的项目现在应该可以按预期运行。