Android 如何让拨号器打开并显示电话号码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11699819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:31:53  来源:igfitidea点击:

How do I get the dialer to open with phone number displayed?

androidandroid-intentphone-number

提问by Nik

I don't need to call the phone number, I just need the dialer to open with the phone number already displayed. What Intentshould I use to achieve this?

我不需要拨打电话号码,我只需要拨号器打开,电话号码已经显示。我Intent应该用什么来实现这一目标?

回答by AAnkit

Two ways to achieve it.

有两种方法可以实现。

1) Need to start the dialer via code, without user interaction.

1) 需要通过代码启动拨号器,无需用户交互。

You need Action_Dial,

你需要Action_Dial

use below code it will open Dialer with number specified

使用下面的代码,它将打开指定号码的拨号器

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent); 

The 'tel:' prefix is required, otherwhise the following exception will be thrown: java.lang.IllegalStateException: Could not execute method of the activity.

'tel:' 前缀是必需的,否则将抛出以下异常: java.lang.IllegalStateException: 无法执行活动的方法。

Action_Dial doesn't require any permission.

Action_Dial 不需要任何权限。

If you want to initiate the call directly without user's interaction, You can use action Intent.ACTION_CALL. In this case, you must add the following permission in your AndroidManifest.xml:

如果您想直接发起呼叫而无需用户交互,您可以使用 action Intent.ACTION_CALL。在这种情况下,您必须在 AndroidManifest.xml 中添加以下权限:

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

2) Need user to click on Phone_Number string and start the call.

2) 需要用户点击 Phone_Number 字符串并开始通话。

android:autoLink="phone" 

You need to use TextView with below property.

您需要使用具有以下属性的 TextView。

android:autoLink="phone" android:linksClickable="true" a textView property

android:autoLink="phone" android:linksClickable="true" 一个 textView 属性

You don't need to use intent or to get permission via this way.

您不需要使用意图或通过这种方式获得许可。

回答by ashishduh

Pretty late on the answer, but if you have a TextViewthat you're showing the phone number in, then you don't need to deal with intents at all, you can just use the XML attribute android:autoLink="phone"and the OS will automatically initiate an ACTION_DIALIntent.

答案很晚了,但是如果你有一个TextView你正在显示电话号码的,那么你根本不需要处理意图,你可以只使用 XML 属性android:autoLink="phone",操作系统将自动启动一个ACTION_DIAL意图。

回答by gprathour

Okay, it is going to be extremely late answer to this question. But here is just one sample if you want to do it in Kotlin.

好的,这个问题的答案会非常晚。但如果您想在Kotlin 中进行,这里只是一个示例。

val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:<number>")
startActivity(intent)

Thought it might help someone.

认为它可能会帮助某人。

回答by Ojonugwa Jude Ochalifu

As @ashishduh mentioned above, using android:autoLink="phoneis also a good solution. But this option comes with one drawback, it doesn't work with all phone number lengths. For instance, a phone number of 11 numbers won't work with this option. The solution is to prefix your phone numbers with the country code.

正如上面提到的@ashishduh,使用android:autoLink="phone也是一个很好的解决方案。但此选项有一个缺点,它不适用于所有电话号码长度。例如,包含 11 个号码的电话号码不适用于此选项。解决方案是在您的电话号码前加上国家/地区代码。

Example:

例子:

08034448845won't work

08034448845不会工作

but +2348034448845will

+2348034448845

回答by saurabh yadav

<TextView
 android:id="@+id/phoneNumber"
 android:autoLink="phone"
 android:linksClickable="true"
 android:text="+91 22 2222 2222"
 />

This is how you can open EditText label assigned number on dialer directly.

这就是如何直接在拨号器上打开 EditText 标签分配的号码。

回答by Pouria Hemati

Add permission in manifest :

在清单中添加权限:

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

Make button or any widget example : btnCall

制作按钮或任何小部件示例: btnCall

btnCall.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent callIntent = new Intent(Intent.ACTION_DIAL);
        callIntent.setData(Uri.parse("tel: +98990*******");
        startActivity(callIntent);

    }
});