如何在Android中使用Intent拨打电话?

news/2024/10/18 9:22:26/

本文翻译自: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中,对于某些功能,您需要将权限添加到清单文件中。

  1. Go to the Projects AndroidManifest.xml 转到项目AndroidManifest.xml
  2. Click on the Permissions Tab 单击权限选项卡
  3. Click on Add 点击添加
  4. Select Uses Permission 选择使用权限
  5. See the snapshot below 见下面的快照 在此处输入图片说明

6.Save the manifest file and then run your project. 6.保存清单文件,然后运行您的项目。 Your project now should run as expected. 您的项目现在应该可以按预期运行。


http://www.ppmy.cn/news/462005.html

相关文章

无法在Android手机上拨打或接听电话,如何解决

使用Android&#xff0c;拨打和接听电话变得更加容易。您可以在几分之一秒内与其他联系人建立联系。但是&#xff0c;一些用户在无法在Android手机上拨打或接听电话时不断抱怨这些情况。在这样的时刻&#xff0c;当你看到周围的每个人都忙于他们的智能手机&#xff0c;你甚至不…

android拨打电话无法跳转到通话界面

最近客户要求我们用谷歌的dialer替换原生的dialer,替换之后发现在拨打电话的时候无法跳转到指定的界面.通过阅读源码发现,telecom固定的了跳转的界面. packages/services/Telecomm/src/com/android/server/telecom/InCallController.java public InCallController(Context con…

python 实现手机自动拨打电话(通话压力测试)

现在能用自动化实现的&#xff0c;尽量使用自动化程序去操作&#xff0c;代替人工去操作&#xff0c;更有效率。 今天说下用python结合adb命令去实现安卓手机端的通话压力测试。 #操作前先在设置里打开power键可以结束通话按钮&#xff0c;否则会导致代码报错 from time impor…

Android如何判断手机是否处于正在通话中,通话状态的判断

1. 如何判断手机是否处于正在通话中&#xff1f;&#xff08;任何时候都可以判断手机是否处于通过中&#xff09; private boolean phoneIsInUse() { boolean phoneInUse false; try { ITelephony phone ITelephony.Stub.asInterface(ServiceManager.checkService("pho…

小米8android系统,小米8:2021年,是时候说再见了!

众所周知&#xff0c;一款安卓手机的使用周期大概是在2-3年。在这之后&#xff0c;很多用户都会选择更新换代。小米8&#xff0c;作为小米发布已经两年半的机型&#xff0c;现在也到了要和我们说再见的时候了。那么&#xff0c;作为小米曾经的数字旗舰&#xff0c;小米8还能不能…

小米8se怎么解屏幕锁_小米8 SE解锁system系统分区教程_小米8SE如何一键解锁系统分区...

下面也是咱们的这个小米8 SE手机解锁system系统分区的教程了&#xff0c;看到有不少的机友想对自己的手机解锁系统分区&#xff0c;可是不知道从哪里入手&#xff0c;这个解锁分区的教程看似复杂&#xff0c;其实也挺简单的&#xff0c;前提条件是你的手机已经进行过root操作了…

Android 8 WiFi断流,小米8wifi断流怎么解决

大家好&#xff0c;我是时间财富网智能客服时间君&#xff0c;上述问题将由我为大家进行解答。 小米8wifi断流解决的方法如下&#xff1a; 1、下拉手机通知栏&#xff0c;按住WiFi。 2、选取一个WiFi连接&#xff0c;点击设置。 3、点击WLAN&#xff0c;点击底部高级设置。 4、…

小米8 android p 刘海,小米8刘海里面竟然藏了这么多黑科技,丑也值了

原标题&#xff1a;小米8刘海里面竟然藏了这么多黑科技&#xff0c;丑也值了 小米8在深圳发布时&#xff0c;雷军现场公布米8用的是刘海全面屏时&#xff0c;现场一片寂静&#xff0c;很多米粉可能对刘海屏是不满意的&#xff0c;因为颜值问题&#xff0c;不过雷军此举也是无奈…