以编程方式在Android上拨打电话号码

30

如何在Android应用程序中以编程方式拨打号码?我不想直接拨打电话,因为我知道可以通过创建一个意图:new Intent(Intent.ACTION_CALL)来实现。我只是想通过传递号码到意图中,使用户能够进入Android拨号提示界面,并将号码预先输入,以便她有选择是否拨打该号码的选项。


1
再次强调,该链接描述的是如何进行通话,而不是如何拨打电话号码。 - Sergo Pasoevi
6个回答

61

使用以下代码:

Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);

2
您可以使用以下代码从您的活动中提供输入号码以拨打电话,但不能在@user370305提供的答案中提供拨号号码。 - Systematix Infotech
@fremmedehenvendelser 这是一个完整的代码片段,与 user370305 提供的不同。他提到在答案中添加数字,但在代码中并没有这样做。 - Dheeraj Bhaskar
@AngelJanniee,它可能无法工作是因为您在清单中忘记声明CALL_PHONE权限,或者您可能已经忘记在Android 6.0+上运行时请求权限。 - Akah

10

使用ACTION_DIAL

例如,

Intent intent = new Intent(Intent.ACTION_DIAL);

Activity Action: 根据数据指定的号码拨打电话。这将显示一个UI界面并拨出指定的号码,允许用户明确地发起呼叫。


8
Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
context.startActivity(intent);

不要忘记在您的清单文件中添加相关权限:

<uses-permission android:name="android.permission.CALL_PHONE" />

1
我问的是如何“拨号”,而不是如何打电话。 - Sergo Pasoevi
虽然有些离题,但我很高兴他发布了这个答案。这正是我正在寻找的。 - Dwebtron

1
这段代码将在没有用户交互的情况下发起通话,并同时启动默认的电话拨号器。
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + url));
    intent.setClassName("com.android.phone","com.android.phone.OutgoingCallBroadcaster");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

如果您希望用户选择其拨号器,请删除。
intent.setClassName("com.android.phone","com.android.phone.OutgoingCallBroadcaster");

如果您只想将号码输入到拨号器中,用户按下呼叫按钮,请使用以下代码(我相信这就是您所要求的)。
    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + url));
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

其中url是电话号码,不要忘记在清单文件中写入权限。

    <uses-permission android:name="android.permission.CALL_PHONE" />

请参阅Systematix Infotech的回答。 - Sergo Pasoevi
它怎么样? - Ahmed Samir

0

我曾经遇到过类似的问题,后来解决了。

        btn.setOnClickListener(new  View.OnClickListener(){
        public void onClick(View v) {
            String q=(t.getText().toString());
            Uri u=Uri.parse("tel:"+q);

            Intent i=new Intent(Intent.ACTION_VIEW,u);
            startActivity(i);
        }
    });

0

这段代码将在没有手机默认拨号键盘的情况下拨打您的号码:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + textView.getText())); 
intent.setClassName("com.android.phone","com.android.phone.OutgoingCallBroadcaster");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接