【发布时间】:2012-10-29 20:11:00
【问题描述】:
我想在 android 中以编程方式显示数字拨号键盘(电话呼叫)。代码可用于直接号码拨号,但我只需要在单击按钮时显示拨号键盘。
【问题讨论】:
标签: android buttonclick phone-call numeric-keypad
我想在 android 中以编程方式显示数字拨号键盘(电话呼叫)。代码可用于直接号码拨号,但我只需要在单击按钮时显示拨号键盘。
【问题讨论】:
标签: android buttonclick phone-call numeric-keypad
如果您想在不插入任何号码的情况下以编程方式打开拨号器,则可以使用此代码:
Java
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
科特林
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);
科特林
val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:123 456789")
startActivity(intent)
【讨论】:
这是不同的,但如果你想通过点击一个数字来访问你的拨号盘,请在你的 xml 中声明自动链接属性
android:autoLink="phone"
【讨论】:
如果你想在非活动类中使用它,那么创建一个这样的函数:
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());
【讨论】:
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
它将显示拨号窗口检查here 以获取信息
【讨论】:
public void openDialPad(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(phoneNumber));
startActivity(intent);
}
【讨论】:
制作按钮或任何小部件示例: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" />
【讨论】:
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>
【讨论】:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:9999999999"));
startActivity(intent);
为此,我们不需要在 AndroidManifest.xml
中添加任何权限【讨论】: