【问题标题】:android studio calling buttonandroid studio 通话按钮
【发布时间】:2015-12-15 18:06:06
【问题描述】:

我正在尝试制作一个点击按钮,该按钮在按下时拨打电话。 这是我的java代码:

public void CampusSafClick(View view){
    Intent callIntent =new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:6038994210"));
    startActivity(callIntent);
}

我了解如何制作 onclick 按钮,所以这不是问题。

我在清单中有这段代码:

<uses-permission android:name="android.permision.CALL_PHONE"></uses-permission>

我不断收到错误消息很遗憾,您的应用程序已停止运行。

【问题讨论】:

  • 应用不断崩溃。
  • 能否分享您的 xml 并完成活动
  • 您有日志或错误消息吗?这将有助于缩小范围。
  • 我不断收到错误消息:不幸的是,您的应用已停止工作。
  • 什么类型的错误类似于权限错误或发布完整的错误消息

标签: java android button call


【解决方案1】:

这是工作代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialContactPhone("123123123");
        }
    });
}

private void dialContactPhone(final String phoneNumber) {
    startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}

【讨论】:

  • 感谢 Nikola 的代码。我只需要将使用权限放在应用程序列表上方而不是在其中。
【解决方案2】:

我需要在清单的应用列表上方输入代码:

<uses-permission android:name="android.permission.CALL_PHONE"/>

【讨论】:

    【解决方案3】:

    你需要Action_Dial

    使用下面的代码将打开指定号码的经销商。

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

    tel:前缀是必须的,否则会抛出以下异常:java.lang.IllegalStateException: Could not execute method of the activity.

    Action_Dial 不需要任何权限。

    如果您想直接发起呼叫而无需用户交互,您可以使用操作Intent.ACTION_CALL。在这种情况下,您必须在AndroidManifest.xml 中添加以下权限:

    <uses-permission android:name="android.permission.CALL_PHONE" />
    

    【讨论】:

      【解决方案4】:

      对于使用android拨打电话,可以使用Intents来实现。

      public void MakePhoneCall(View view){
          Intent callIntent =new Intent(Intent.ACTION_CALL);
          callIntent.setData(Uri.parse("tel:9961907453"));
          if (ActivityCompat.checkSelfPermission(MainActivity.this,
                 Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                 }
                 startActivity(callIntent);
      }
      

      我在清单中有这段代码:

      <uses-permission android:name="android.permision.CALL_PHONE"></uses-permission>
      

      如果您使用高于 Lolipop 的 SDK 版本,则应包含请求权限。

      【讨论】:

        【解决方案5】:

        您可以使用以下代码

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

        并包含在清单文件中

        <uses-permission android:name="android.permission.CALL_PHONE"/>
        

        【讨论】:

          【解决方案6】:
          ivCall.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (serviceListDates.get(position).getUser_mobile().length() != 0) {
                      final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
                      alertDialog.setTitle("NKA SERVICE");
                      alertDialog.setMessage("Do you want to Call ?");
                      alertDialog.setIcon(R.drawable.call_icon);
                      alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              ((DeliveredServiceOilActivity) mContext).callPhoneNumber
                                      (serviceListDates.get(position).getUser_mobile());
                          }
                      });`enter code here
                      alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              dialog.cancel();
                          }
                      });
                      alertDialog.show();
                  } else
                      AlertUtils.SHOW_TOAST(mContext, mContext.getString(R.string.please_add_number));
              }
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-10-17
            • 2015-12-19
            • 1970-01-01
            • 2023-03-28
            • 1970-01-01
            • 1970-01-01
            • 2017-03-15
            • 2016-02-17
            相关资源
            最近更新 更多