【问题标题】:make a phone call click on a button拨打电话 点击按钮
【发布时间】:2024-05-01 20:35:02
【问题描述】:

当我按下 android 中的按钮时,我正在尝试拨打电话

((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    String phno="10digits";

    Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse(phno));
    startActivity(i);
  }
});

但是当我运行并单击按钮时,它给了我错误

ERROR/AndroidRuntime(1021): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CALL dat=9392438004 }

我该如何解决这个问题?

【问题讨论】:

  • 你在模拟器上试试这个吗?用真机试试。模拟器可能会错过调用者活动。

标签: android button phone-call


【解决方案1】:

您是否在清单文件中授予了权限

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

在你的活动中

  Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:123456789"));
    startActivity(callIntent);

如果您发现任何问题,请告诉我。

【讨论】:

  • 绝对没有问题。哇!!!我现在正在学习为 Android 编写应用程序,这个问题早在 2011 年就被问到了。感谢您的简短而甜蜜的回答。
  • 您不应该使用 Intent.ACTION_CALL,除非您正在编写拨号应用程序。相反,您应该使用 Intent.ACTION_DIAL 使用您传递的号码预先填充拨号器。解决此问题的正确方法是在您要拨打的 10 位数号码前添加“tel:”。
  • 10 位数字?所以你不能用国际前缀拨打手机,比如+491702112334?因为这是 13 位数字
  • 我知道这很旧,但我正在为旧硬件做一些事情。我在 android 2,1 (api 7) 平台上收到“未找到活动”异常。有没有办法从这个平台拨号?
  • 使用ACTION_DIAL时无需添加权限
【解决方案2】:

调用/开始调用有两种意图:ACTION_CALL 和 ACTION_DIAL。

ACTION_DIAL只会打开填写的dialer with the number,但允许用户实际打开call or reject the callACTION_CALL 会立即拨打该号码并需要额外的权限

所以请确保您有权限

uses-permission android:name="android.permission.CALL_PHONE"

在您的 AndroidManifest.xml

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dbm.pkg"
    android:versionCode="1"
    android:versionName="1.0">

    <!-- NOTE! Your uses-permission must be outside the "application" tag
               but within the "manifest" tag. -->

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

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">

        <!-- Insert your other stuff here -->

    </application>

    <uses-sdk android:minSdkVersion="9" />
</manifest> 

【讨论】:

  • 嗨,我有一个关于这个答案的问题。如果我必须拨打国际电话号码,那么我应该在哪里提及国家代码?
  • 我无法再访问此代码。抱歉,这可能会有所帮助*.com/questions/13132724/…
【解决方案3】:

以上都没有工作,所以稍微修改一下这里的代码对我有用

        Intent i = new Intent(Intent.ACTION_DIAL);
        String p = "tel:" + getString(R.string.phone_number);
        i.setData(Uri.parse(p));
        startActivity(i);

【讨论】:

    【解决方案4】:

    将您的字符串更改为String phno="tel:10digits"; 并重试。

    【讨论】:

    • 感谢 Harshad,但我无法获得任何可以直接呼叫该号码的键盘,而无需用户修改该号码
    • 你想不显示拨号盘直接打电话???如果是这样,那么使用Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(phno));
    • 如上直接调用将不起作用,除非您将此行添加为清单文件&lt;uses-permission android:name="android.permission.CALL_PHONE"&gt;&lt;/uses-permission&gt;中的清单的子项@
    • 这个想法有效吗?如果正确,您可以接受答案并帮助其他人..
    • 如何直接与电话中未提供的特定电话进行视频通话:10digit no,来自android的双人视频通话请帮助我。
    【解决方案5】:

    我也玩得很开心。我没有意识到除了额外的权限之外,您还需要将“tel:”附加到其中包含数字的字符串上。这就是我的功能正常后的样子。希望这可以帮助。

    @Override
    public void onClick(View v) {
      Intent intent = new Intent(Intent.ACTION_DIAL);
      String temp = "tel:" + phone;
      intent.setData(Uri.parse(temp));
    
      startActivity(intent);
    }

    【讨论】:

    • 有帮助的@JCrooks,大多数用户不允许权限,这将在这种情况下有所帮助。
    【解决方案6】:

    之前检查权限(适用于 android 6 及更高版本):

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) ==
                                PackageManager.PERMISSION_GRANTED) 
       {
    
         context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:09130000000")));
       }
    

    【讨论】:

      【解决方案7】:

      在您的意图中添加“电话:”以及要拨打的号码,然后开始您的活动。

      Intent myIntent = new Intent(Intent.ACTION_CALL);
       String phNum = "tel:" + "1234567890";
       myIntent.setData(Uri.parse(phNum));
        startActivity( myIntent ) ;
      

      【讨论】:

        【解决方案8】:

        要将代码放在一行中,请尝试以下操作:

        startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:123456789")));
        

        连同适当的清单权限:

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

        希望这会有所帮助!

        【讨论】:

          【解决方案9】:

          对于那些使用 AppCompact...试试这个

          import android.support.v7.app.AppCompatActivity;
          import android.os.Bundle;
          import android.content.Intent;
          import android.util.Log;
          import android.view.View;
          import android.widget.Button;
          import android.widget.EditText;
          import android.widget.Toast;
          import android.net.Uri;
          
          public class MainActivity extends AppCompatActivity {
          
          
          
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  Button startBtn = (Button) findViewById(R.id.db);
                  startBtn.setOnClickListener(new View.OnClickListener() {
                      public void onClick(View view) {
                          makeCall();
                      }
                  });
          
              }
          
              protected void makeCall() {
                  EditText num = (EditText)findViewById(R.id.Dail);
                  String phone = num.getText().toString();
                  String d = "tel:" + phone ;
                  Log.i("Make call", "");
                  Intent phoneIntent = new Intent(Intent.ACTION_CALL);
                  phoneIntent.setData(Uri.parse(d));
                  try {
                      startActivity(phoneIntent);
                      finish();
                      Log.i("Finished making a call", "");
                  } catch (android.content.ActivityNotFoundException ex) {
                      Toast.makeText(this, "Call faild, please try again later.", Toast.LENGTH_SHORT).show();
                  }
              }
          
          }
          

          然后将此添加到您的清单中,,,

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

          【讨论】:

            【解决方案10】:

            还可以检查设备是否支持电话

            private boolean isTelephonyEnabled(){
            TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
            return tm != null && tm.getSimState()==TelephonyManager.SIM_STATE_READY
            }
            

            【讨论】:

              【解决方案11】:
              I hope, this short code is useful for You,
                 ## Java Code ##
               startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+txtPhn.getText().toString())));
              
              
              
              ----------------------------------------------------------------------
              
              
              Please check Manifest File,(for Uses permission)
              ## Manifest.xml ##
              <manifest
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  package="com.dbm.pkg"
                  android:versionCode="1"
                  android:versionName="1.0">
              
                  <!-- NOTE! Your uses-permission must be outside the "application" tag
                             but within the "manifest" tag. -->
              ## uses-permission for Making Call ##
                  <uses-permission android:name="android.permission.CALL_PHONE" />
              
                  <application
                      android:icon="@drawable/icon"
                      android:label="@string/app_name">
              
                      <!-- Insert your other stuff here -->
              
                  </application>
              
                  <uses-sdk android:minSdkVersion="9" />
              </manifest> 
              

              【讨论】:

                【解决方案12】:

                我刚刚在 Android 4.0.2 设备 (GN) 上解决了这个问题,并且适用于该设备/版本的唯一版本类似于第一个 5 星并具有 CALL_PHONE 权限和答案:

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:123456789"));
                startActivity(callIntent);
                

                使用任何其他解决方案,我在此设备/版本上得到了 ActivityNotFoundException。 旧版本怎么样?有人会提供反馈吗?

                【讨论】:

                  【解决方案13】:

                  经许可:

                  Intent callIntent = new Intent(Intent.ACTION_CALL);
                  callIntent.setData(Uri.parse("tel:9875432100"));
                  if (ActivityCompat.checkSelfPermission(yourActivity.this,android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                  if (ActivityCompat.shouldShowRequestPermissionRationale(yourActivity.this,
                                          android.Manifest.permission.CALL_PHONE)) {
                                  } else {
                                      ActivityCompat.requestPermissions(yourActivity.this,
                                              new String[]{android.Manifest.permission.CALL_PHONE},
                                              MY_PERMISSIONS_REQUEST_CALL_PHONE);
                                  }
                              }
                  startActivity(callIntent);
                  

                  【讨论】: