android打电话的intent,如何在Android中使用intent打电话?

如何在Android中使用intent打电话?

我正在使用以下代码在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);

权限

例外

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)

19个解决方案

333 votes

您可以使用Intent.ACTION_DIAL而不是Intent.ACTION_CALL.这会显示已输入号码的拨号器,但允许用户决定是否实际拨打电话。 ACTION_DIAL不需要CALL_PHONE的许可。

Ridcully answered 2019-02-06T23:57:23Z

195 votes

这个演示对你有帮助......

通话按钮单击:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));

startActivity(intent);

清单许可:

Denny Sharma answered 2019-02-06T23:58:00Z

121 votes

更优雅的选择:

String phone = "+34666777888";

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));

startActivity(intent);

cprcrack answered 2019-02-06T23:58:24Z

67 votes

一切都好。

我刚在清单文件中的应用程序标记之前放置了调用权限标记

现在每件事情都很好。

UMAR answered 2019-02-06T23:59:00Z

41 votes

在您的意图中使用ACTION_DIAL操作,这样您就不需要任何权限。 您需要ACTION_CALL权限的原因是在没有用户任何操作的情况下拨打电话。

Intent intent = new Intent(Intent.ACTION_DIAL);

intent.setData(Uri.parse("tel:0987654321"))

startActivity(intent);

wheeliez answered 2019-02-06T23:59:25Z

20 votes

只需简单的oneliner,无需任何额外的权限:

private void dialContactPhone(final String phoneNumber) {

startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));

}

Artemiy answered 2019-02-06T23:59:49Z

13 votes

使用此完整代码

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);

Osama Ibrahim answered 2019-02-07T00:00:13Z

11 votes

请求清单中的权限

用于调用使用此代码

Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:99xxxxxxxx"));

try {

startActivity(in);

} catch (android.content.ActivityNotFoundException ex) {

Toast.makeText(mcontext, "Could not find an activity to place the call.", Toast.LENGTH_SHORT).show();

}

Abhay Anand answered 2019-02-07T00:00:43Z

11 votes

重要的提示:

如果您使用CALL_PHONE,则必须添加CALL_PHONE权限。

只有当你不希望你的应用程序出现在没有SIM卡或没有GSM的平板电脑上时,它才会出现问题。

在你的活动中:

Intent callIntent = new Intent(Intent.ACTION_CALL);

callIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));

startActivity(callIntent);

表现:

因此,如果它不是您应用的关键功能,请尽量远离添加CALL_PHONE权限。

其他解决方案

是显示电话应用程序的屏幕上写的数字,因此用户只需要点击通话按钮:

Intent dialIntent = new Intent(Intent.ACTION_DIAL);

dialIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));

startActivity(dialIntent);

不需要许可。

MBH answered 2019-02-07T00:01:59Z

7 votes

权限:

意图:

Intent callIntent = new Intent(Intent.ACTION_CALL);

callIntent.setData(Uri.parse("tel:0377778888"));

startActivity(callIntent);

answered 2019-02-07T00:02:25Z

5 votes

你也可以使用它:

String uri = "tel:" + posted_by.replaceAll("[^0-9|\\+]", "");

Áron Pável answered 2019-02-07T00:02:49Z

5 votes

AndroidManifest.xml中的权限

完整代码:

private void onCallBtnClick(){

if (Build.VERSION.SDK_INT < 23) {

phoneCall();

}else {

if (ActivityCompat.checkSelfPermission(mActivity,

Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {

phoneCall();

}else {

final String[] PERMISSIONS_STORAGE = {Manifest.permission.CALL_PHONE};

//Asking request Permissions

ActivityCompat.requestPermissions(mActivity, PERMISSIONS_STORAGE, 9);

}

}

}

@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

boolean permissionGranted = false;

switch(requestCode){

case 9:

permissionGranted = grantResults[0]== PackageManager.PERMISSION_GRANTED;

break;

}

if(permissionGranted){

phoneCall();

}else {

Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();

}

}

private void phoneCall(){

if (ActivityCompat.checkSelfPermission(mActivity,

Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {

Intent callIntent = new Intent(Intent.ACTION_CALL);

callIntent.setData(Uri.parse("tel:12345678900"));

mActivity.startActivity(callIntent);

}else{

Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();

}

}

Atif Mahmood answered 2019-02-07T00:03:20Z

3 votes

为避免这种情况,可以使用GUI输入权限。 Eclipse负责插入权限标记的位置,更常见的是不正确

SaKet answered 2019-02-07T00:03:45Z

1 votes

在Android中,您需要为Manifest文件添加权限。

转到Projects AndroidManifest.xml

单击“权限”选项卡

单击“添加”

选择使用权限

请参阅下面的快照

8ea2163cf010bd093a25a9346fd56288.png

6.保存清单文件,然后运行您的项目。您的项目现在应该按预期运行。

MindBrain answered 2019-02-07T00:04:43Z

1 votes

11-25 14:47:01.681: ERROR/AndroidRuntime(302): blah blah...requires android.permission.CALL_PHONE

^答案在于异常输出“requires android.permission.CALL_PHONE”:)

Charlie Scott-Skinner answered 2019-02-07T00:05:08Z

1 votes

final public void Call(View view){

try {

EditText editt=(EditText)findViewById(R.id.ed1);

String str=editt.getText().toString();

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+str));

startActivity(intent);

}

catch (android.content.ActivityNotFoundException e){

Toast.makeText(getApplicationContext(),"App failed",Toast.LENGTH_LONG).show();

}

Deepak Singh answered 2019-02-07T00:05:26Z

1 votes

if(ContextCompat.checkSelfPermission(

mContext,android.Manifest.permission.CALL_PHONE) !=

PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions((Activity) mContext, new

String[]{android.Manifest.permission.CALL_PHONE}, 0);

} else {

startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Number")));

}

Mohsinali answered 2019-02-07T00:05:44Z

1 votes

要使用意图进行调用活动,您应该请求适当的权限。

为此,您在AndroidManifest.xml文件中包含使用权限。

然后在您的活动中包含以下代码

if (ActivityCompat.checkSelfPermission(mActivity,

Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {

//Creating intents for making a call

Intent callIntent = new Intent(Intent.ACTION_CALL);

callIntent.setData(Uri.parse("tel:123456789"));

mActivity.startActivity(callIntent);

}else{

Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();

}

Codemaker answered 2019-02-07T00:06:22Z

-36 votes

我认为您只需要在清单中添加权限:

这将摆脱你的安全例外。

raghav chopra answered 2019-02-07T00:06:53Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值