首页 > 解决方案 > Android 呼叫电话号码侧边菜单

问题描述

我正忙于制作我的第一个 android 应用程序,我正在寻找创建以下选项:当有人从“activit_main_drawer.xml”点击“联系我们”时,他们将直接拨打“电话号码”

有谁能帮助我吗?:)

这就是“按钮”的调用方式

    <item
        android:id="@+id/nav_belnu"
        android:icon="@drawable/ic_phone_square_solid"
        android:title="BEL nu" />

标签: androidcall

解决方案


在你的按钮的 onclick 监听器中,你可以这样进行,记得在你的项目的清单文件中添加调用权限:

private void makeCall(String phoneNumber){
        try {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + phoneNumber));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                    return;
                }
                startActivity(intent);
            }catch (Exception e)
            {
                e.printStackTrace();
            }
}

推荐阅读