10

ActivityNotFoundException在执行 Intent.ACTION_CALL 操作时得到。我找到了很多链接,但都无法解决我的问题。

它有时会给我例外,例如

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CALL dat=tel:xxx-xxx-xxxx pkg=com.android.phone (has extras) }

我在我的项目中使用了以下代码

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

4 回答 4

14

无法保证可以处理给定的意图(即平板电脑可能根本没有电话应用程序)。如果没有匹配的应用程序intent-filter,那么你将面临ActivityNotFoundException。正确的方法是意识到这一点并使用它try/catch来克服崩溃并正确恢复:

try {
   String contact_number="123456789";
   Intent callIntent = new Intent(Intent.ACTION_CALL);
   callIntent.setData(Uri.parse("tel:" + contact_number));
   startActivity(callIntent);
} catch (Exception e) {
   // no activity to handle intent. show error dialog/toast whatever
}

此外,您应该ACTION_DIAL改为使用,因为ACTION_CALL需要额外的许可。

于 2015-01-26T08:32:31.693 回答
13

在开始之前,您应该检查是否有任何活动处理此意图。如果您的应用在仅具有 wifi 且没有电话功能的平板电脑上运行,则可能会发生这种情况。此外,如果您只打算启动拨号程序,则最好使用直接从您的应用程序拨打电话ACTION_DIALACTION_CALL

final Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"));
if (dialIntent.resolveActivity(context.getPackageManager()) != null) {
    // put your logic to launch call app here
  }
于 2015-01-26T08:09:50.663 回答
6

我已经解决了这个问题。我使用了以下代码

String contact_number="123456789";
Intent callIntent = new Intent(Intent.ACTION_CALL);
intent.setPackage("com.android.phone");
callIntent.setData(Uri.parse("tel:" + contact_number));
startActivity(callIntent);

我已经用 Lollipop 替换了这条线

intent.setPackage("com.android.phone");

intent.setPackage("com.android.server.telecom");
于 2015-01-26T09:21:15.837 回答
0

这是我的代码。

if (BuildUtil.isAndroidM()) {
        if (ActivityCompat.checkSelfPermission(mActivity,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(mActivity, PermissionUtil.requestPermissionFineAndCoarse(),
                    PHONE_REQUEST_CODE);
        }
    } else {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        if (BuildUtil.isAndroidL()) {
            callIntent.setPackage("com.android.server.telecom");
        } else {
            callIntent.setPackage("com.android.phone");
        }
        callIntent.setData(Uri.parse("tel:" + phone));
        startActivity(callIntent);
    }
于 2016-07-20T03:52:53.830 回答