如何在安卓系统中通过编程方式打开数字拨号键盘?

21

我想在Android中通过点击按钮编程方式显示数字拨号键盘(电话呼叫)。已经有用于直接号码拨打的代码,但我只需要在点击按钮时显示拨号键盘。

9个回答

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

对此,我们不需要在 AndroidManifest.xml 中添加任何权限。


对于 ACTION_DIAL,我们不需要 CALL_PHONE 权限。它只需要用于 ACTION_CALL。 - Thamilan S

28

如果您想在没有插入任何号码的情况下以编程方式打开拨号器,可以使用此代码:

Java

Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
Kotlin.
val intent = Intent(Intent.ACTION_DIAL)
startActivity(intent)
如果您需要打开带有已经输入的号码的拨号器,可以使用以下代码:

Java

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123 456789"));
startActivity(intent);
Kotlin
val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:123 456789")
startActivity(intent)

4
Intent intent =  new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);

它将展示拨号窗口检查,此处提供相关信息。


3
创建按钮或其他窗口部件示例:button1
  button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_DIAL);
            callIntent.setData(Uri.parse("tel:"+button1.getText().toString().trim()));
            startActivity(callIntent);

        }
    });

在清单文件中添加权限:
 <uses-permission android:name="android.permission.CALL_PHONE" />

2
 public void openDialPad(String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse(phoneNumber));
        startActivity(intent);
    }

1
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
 if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
startActivity(callIntent);

此外,您应该在清单文件中按照以下方式注册自定义拨号屏幕:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
    android:name=".MyDialerApplication"
    android:label="@string/app_name" >

    <intent-filter android:priority="100" >
        <action android:name="android.intent.action.MAIN" />
         <action android:name="android.intent.action.DIAL" />
         <action android:name="android.intent.action.CALL_PRIVILEGED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="tel" />

    </intent-filter>
</activity>


0

打开拨号器并输入指定手机号的最简代码:

val intent = Intent(Intent.ACTION_DIAL,Uri.parse("tel:$text"))
startActivity(intent)

0
如果您想在非活动类中使用它,则创建一个如下的函数:
package bp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import session.MyApplication;

/**
 * Created by Atiar Talukdar on 7/11/2019.
 */
public class Utils {

    public static void openDialPad(Activity activity, String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        activity.startActivity(intent);
    }
}

然后可以从任何地方调用,例如:

Utils.openDialPad(getActivity(),data.getContactNo());

或者

Utils.openDialPad(this,data.getContactNo());


0

这是不同的,但如果你想通过点击一个数字来访问你的拨号键盘,在你的xml中,声明autolink属性

android:autoLink="phone"

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