拨打电话:点击按钮

54

我正在尝试在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 }

我该如何解决这个问题?


1
你在模拟器上试过了吗?试试真机。模拟器可能会错过呼叫活动。 - Vladimir Ivanov
14个回答

146

你在清单文件中授予权限了吗?

 <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);

如果你发现任何问题,请让我知道。


2
完全没有任何问题。哇!!!我现在正在学习编写Android应用程序,而这个问题是在2011年提出的。感谢您提供这个简短而甜美的答案。 - ThN
24
除非你正在编写一个拨号应用程序,否则不应使用Intent.ACTION_CALL。相反,你应该使用Intent.ACTION_DIAL,它会在拨号器中预填你传递的号码。解决这个问题的正确方法是在想要拨打的10位数字前面加上"tel:"。 - David Shellabarger
10位数字?那么你不能拨打带有国际前缀的手机号码,例如+491702112334,因为它是13位数字。 - Phantômaxx
我知道这很老,但我正在为旧硬件做一些事情。 在Android 2.1(API 7)平台上遇到了“未找到活动”异常。有没有办法从这个平台拨号? - Justin
使用ACTION_DIAL时无需添加权限。 - gonephishing

16

有两种意图可以调用/开始通话:ACTION_CALL和ACTION_DIAL。

ACTION_DIAL仅会打开带有号码的拨号器,但允许用户实际上进行呼叫或拒绝呼叫。ACTION_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> 

你好,我有一个关于这个答案的问题。如果我需要拨打国际电话号码,那么我应该在哪里提到国家代码? - Nevaeh
我不再有访问此代码的权限。很抱歉,但这可能会有所帮助 http://stackoverflow.com/questions/13132724/checking-if-call-being-made-is-international - Zar E Ahmer

13

以上方法都不起作用,所以经过一些调整,以下是为我工作的代码

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

12

将您的字符串更改为String phno =“tel:10digits”;,然后重试。


谢谢Harshad,但我无法获取任何键盘,它可以直接拨打该号码,而无需用户修改该号码。 - prasad.gai
你想直接拨打电话而不显示拨号键盘吗?如果是的话,那么请使用 Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(phno)); - Harshad
直接调用上述代码将不起作用,除非您将以下行作为清单文件中的子项添加到清单中:<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>。 - Harshad
这个程序是否有效?如果正确的话,您可以接受答案并帮助其他人。 - Harshad
请帮我解决一个问题,如何直接通过tel:10位号码向特定电话号码进行Duo视频通话,我使用的是Android系统。 - Harsha

8

我也曾遇到类似问题,原来除了需要额外的权限之外,在包含电话号码的字符串前还需要加上"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);
}


1
有用的@JCrooks,大多数用户不允许权限,这将有助于解决这种情况。 - Abdullah Sheikh

5

在 Android 6 及以上版本中,请先检查权限:

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) ==
                            PackageManager.PERMISSION_GRANTED) 
   {

     context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:09130000000")));
   }

4

在你的意图中加上 "tel:" 以及要拨打的电话号码,然后启动你的活动。

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

4

将代码放在一行中,请尝试以下方法:

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

在正确的清单权限下:

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

希望这能有所帮助!

2

对于使用 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" />

1
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> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接