我想在Android上点击按钮,以编程方式显示数字拨号键盘(电话)。代码可用于直接拨号,但我只需要在单击按钮时显示拨号键盘。
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:9999999999"));
startActivity(intent);
为此,我们不需要在AndroidManifest.xml中添加任何权限
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
它将显示拨号窗口检查here的信息
制作按钮或任何小部件示例: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" />
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>
如果要在非活动类中使用它,则创建如下函数:
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());