我想在Android中通过点击按钮编程方式显示数字拨号键盘(电话呼叫)。已经有用于直接号码拨打的代码,但我只需要在点击按钮时显示拨号键盘。
我想在Android中通过点击按钮编程方式显示数字拨号键盘(电话呼叫)。已经有用于直接号码拨打的代码,但我只需要在点击按钮时显示拨号键盘。
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:9999999999"));
startActivity(intent);
对此,我们不需要在 AndroidManifest.xml 中添加任何权限。
如果您想在没有插入任何号码的情况下以编程方式打开拨号器,可以使用此代码:
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);
Kotlinval intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:123 456789")
startActivity(intent)
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
它将展示拨号窗口检查,此处提供相关信息。
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" />
public void openDialPad(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(phoneNumber));
startActivity(intent);
}
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>
打开拨号器并输入指定手机号的最简代码:
val intent = Intent(Intent.ACTION_DIAL,Uri.parse("tel:$text"))
startActivity(intent)
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());
这是不同的,但如果你想通过点击一个数字来访问你的拨号键盘,在你的xml中,声明autolink属性
android:autoLink="phone"