调用Intent.ACTION_CALL时发生ActivityNotFoundException

Noo*_*ani 9 java android activitynotfoundexception

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) }
Run Code Online (Sandbox Code Playgroud)

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

String contact_number="123456789";
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + contact_number));
startActivity(callIntent);
Run Code Online (Sandbox Code Playgroud)

Mar*_*ski 13

无法保证可以处理给定的意图(即平板电脑可能根本没有电话应用程序).如果没有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
}
Run Code Online (Sandbox Code Playgroud)

另外,您应该使用ACTION_DIAL相反的方式,因为ACTION_CALL需要额外的权限.


hid*_*dro 10

在启动之前,您应该检查是否有任何处理此意图的活动.如果您的应用仅在具有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
  }
Run Code Online (Sandbox Code Playgroud)


Noo*_*ani 5

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

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);
Run Code Online (Sandbox Code Playgroud)

我为Lollipop取代了这一行

intent.setPackage("com.android.phone");
Run Code Online (Sandbox Code Playgroud)

intent.setPackage("com.android.server.telecom");
Run Code Online (Sandbox Code Playgroud)