java 调用 Intent.ACTION_CALL 时出现 ActivityNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28146443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActivityNotFoundException when calling Intent.ACTION_CALL
提问by Nooruddin Lakhani
I get ActivityNotFoundException
when perform Intent.ACTION_CALL operation. I found many links but all of it couldn't solve my problem.
我ActivityNotFoundException
在执行 Intent.ACTION_CALL 操作时得到。我找到了很多链接,但所有链接都无法解决我的问题。
It sometimes gives me exception like
它有时会给我例外,例如
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) }
I have used the following code in my project
我在我的项目中使用了以下代码
String contact_number="123456789";
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + contact_number));
startActivity(callIntent);
回答by Marcin Orlowski
There's no guarantee that given intent can be handled (i.e. tablets may not have telephony app at all). If there's no application with matching intent-filter
, then you will face ActivityNotFoundException
. The proper approach is to be aware of this and use try/catch
to defeat the crash and properly recover:
不能保证可以处理给定的意图(即平板电脑可能根本没有电话应用程序)。如果没有匹配的应用程序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
}
Also you should be rather using ACTION_DIAL
instead, as ACTION_CALL
requires additional permission.
此外,您应该ACTION_DIAL
改为使用,因为ACTION_CALL
需要额外的许可。
回答by hidro
You should check if there are any activities that handle this intent before starting it. This can happen if your app run on a tablet with wifi only and has no phone capability. Also if you only intend to launch dialer, better use ACTION_DIAL
than ACTION_CALL
which makes the call directly from your app.
在开始之前,您应该检查是否有任何活动可以处理此意图。如果您的应用程序在只有 wifi 且没有电话功能的平板电脑上运行,就会发生这种情况。此外,如果您只打算启动拨号程序,则ACTION_DIAL
比ACTION_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
}
回答by Nooruddin Lakhani
I have solved this issue. I have used the following code
我已经解决了这个问题。我使用了以下代码
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);
I have replaced this line for Lollipop
我已经用 Lollipop 替换了这条线
intent.setPackage("com.android.phone");
with
和
intent.setPackage("com.android.server.telecom");
回答by raditya gumay
this is my code.
这是我的代码。
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);
}