[英]Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent
I'm getting following error on some devices while opening url.打开 url 时,我在某些设备上遇到以下错误。 I don't get any error on my devices but many devices are.
我的设备上没有任何错误,但很多设备都有。
Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://google.com/... }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2076)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1720)
at android.app.Activity.startActivityForResult(Activity.java:5258)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
at android.app.Activity.startActivityForResult(Activity.java:5203)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560)
at android.app.Activity.startActivity(Activity.java:5587)
at android.app.Activity.startActivity(Activity.java:5555)
The code where error occurs is pretty straight-forward.发生错误的代码非常简单。
Uri uri = Uri.parse("https://google.com");
try {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setShowTitle(true);
CustomTabsIntent customTabsIntent = builder.build();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
ResolveInfo resolveInfo = getPackageManager().resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo != null && resolveInfo.activityInfo.packageName.length() > 0) {
customTabsIntent.intent.setPackage(resolveInfo.activityInfo.packageName);
}
customTabsIntent.launchUrl(this, uri);
} catch (ActivityNotFoundException e) {
// Do nothing
}
In some devices (not old devices, mostly newest device), exception triggered but how can it be possible?在某些设备(不是旧设备,主要是最新设备)中,触发了异常,但怎么可能呢? This means devices has 0 browser ?
这意味着设备有0 个浏览器? If so, my question is how can I open url even user disabled all browsers ?
如果是这样,我的问题是如何打开 url 即使用户禁用了所有浏览器? Thanks in advance.
提前致谢。
Note: Before I upgraded to custom-tabs in the live version, I used following code to open url but I always get the ActivityNotFoundException :注意:在我升级到实时版本的自定义选项卡之前,我使用以下代码打开 url 但我总是得到ActivityNotFoundException :
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(browserIntent);
It is the simplest code the open any url but without usage of try/catch causes crashes.这是打开任何 url 但不使用 try/catch 导致崩溃的最简单代码。 It doesn't matter to use normal way or custom-tabs, it always causes ActivityNotFoundException .
使用普通方式或自定义选项卡都没有关系,它总是会导致ActivityNotFoundException 。 I'm looking a solution which opening url whatever happens.
我正在寻找一个解决方案,无论发生什么都打开 url。 I'm not sure whether it is possible or not.
我不确定这是否可能。
Note2: MinApi is Android 5.0 (Lollipop)注2: MinApi 为 Android 5.0 (Lollipop)
A few notes on this issue:关于这个问题的几点说明:
They query for browsers can detect non-browser applications, which will lead the an ActivityNotFoundException
when launching the Custom Tab.他们查询浏览器可以检测到非浏览器应用程序,这将在启动自定义选项卡时导致
ActivityNotFoundException
。 See this issue for an example.有关示例,请参阅此问题。
When querying for packages that can handle browser intents, I recommend using the below:在查询可以处理浏览器意图的包时,我建议使用以下内容:
Intent browserIntent = new Intent()
.setAction(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setData(Uri.fromParts("http", "", null));
This is also how the Android framework itself detects browsers .这也是Android 框架本身检测浏览器的方式。
The following shouldn't be a problem as the the Custom Tab will still work, but the behaviour of PackageManager.MATCH_DEFAULT_ONLY
changed on Android M and only the default browser is returned - I'm not sure what happens if a default browser is not set, but this may mean the returned list will be empty instead.以下应该不是问题,因为自定义选项卡仍然可以工作,但是
PackageManager.MATCH_DEFAULT_ONLY
的行为在 Android M 上发生了变化,并且只返回了默认浏览器 - 我不确定如果没有设置默认浏览器会发生什么,但这可能意味着返回的列表将为空。 This will mean users will get the disambiguation dialog when a default browser is not set.这意味着当未设置默认浏览器时,用户将获得消歧对话框。 From Android M and above you may also want additionally query the package manager using
PackageManager.MATCH_ALL
if you want to get all browsers avoid the disambiguation dialog.从 Android M 及更高版本中,如果您想让所有浏览器避免消歧对话框,您可能还需要使用
PackageManager.MATCH_ALL
额外查询 package 管理器。
Check out this snippet in the android-browser-helper
library for detecting Custom Tabs and Trusted Web Activity.查看
android-browser-helper
库中的此代码段,以检测自定义选项卡和受信任的 Web 活动。
Android 11 introduced package visibility and applications must now declare which packages they are querying for. Android 11 引入了package 可见性和应用程序现在必须声明它们正在查询哪些包。 If your application is not implementing this, the list returned when querying the package manage will be empty, even with a browser installed.
如果您的应用程序没有实现此功能,则在查询 package 管理时返回的列表将为空,即使安装了浏览器也是如此。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
possibleProviders.addAll(pm.queryIntentActivities(queryBrowsersIntent,
PackageManager.MATCH_ALL));
}
Again, this shouldn't cause an ActivityNotFoundException
as it would just cause the Custom Tab to launch without a package set.同样,这不应导致
ActivityNotFoundException
,因为它只会导致自定义选项卡在没有设置 package 的情况下启动。
Note: Before I upgraded to custom-tabs in the live version, I used following code to open url but I always get the ActivityNotFoundException:
注意:在实时版本中升级到自定义选项卡之前,我使用以下代码打开 url 但我总是得到 ActivityNotFoundException:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri); startActivity(browserIntent);
It is possible for users to disable all browser applications, but I wouldn't expect this to be common.用户可以禁用所有浏览器应用程序,但我不希望这很常见。
Another thing to be aware of is that Android TV and Android Auto devices don't have a browser installed.另一件需要注意的是 Android TV 和 Android Auto 设备没有安装浏览器。 If your application is targeting those devices you won't be able to open these URLs.
如果您的应用程序以这些设备为目标,您将无法打开这些 URL。 But this is only a problem if your app is targeting these platforms.
但这只有在您的应用程序针对这些平台时才会出现问题。
I create a method which based on @andreban's answer.我创建了一个基于@andreban 回答的方法。 I believe it will open url %99.99 times.
我相信它会打开 url %99.99 次。
public static void openURL(Context mContext, Uri uri) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setShowTitle(true);
CustomTabsIntent customTabsIntent = builder.build();
Intent browserIntent = new Intent()
.setAction(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setType("text/plain")
.setData(Uri.fromParts("http", "", null));
List<ResolveInfo> possibleBrowsers;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (possibleBrowsers.size() == 0) {
possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_ALL);
}
} else {
possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
}
if (possibleBrowsers.size() > 0) {
customTabsIntent.intent.setPackage(possibleBrowsers.get(0).activityInfo.packageName);
customTabsIntent.launchUrl(mContext, uri);
} else {
Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, uri);
mContext.startActivity(browserIntent2);
}
}
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=miui.intent.action.RINGTONE_PICKER (has extras) } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1952) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1622) at android.app.Activity.startActivityForResult(Activity.java:4555) at android.app.Activity.startActivityForResult(Activity.java:4513) at android.app.Activity.startActivity(Activity.java:4874) at android.app.Activity.startActivity(Activity.java:4842) at com.android.settings.coolso
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=miui.intent.action.RINGTONE_PICKER (has extras) } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1952) at android.app.Instrumentation.execStartActivity (Instrumentation.java:1622) at android.app.Activity.startActivityForResult(Activity.java:4555) at android.app.Activity.startActivityForResult(Activity.java:4513) at android.app.Activity.startActivity(Activity.java: 4874) at android.app.Activity.startActivity(Activity.java:4842) at com.android.settings.coolso und.widget.BaseItem.performClickSelf(BaseItem.java:6) at com.android.settings.coolsound.widget.BaseItem.onClick(BaseItem.java:1) at android.view.View.performClick(View.java:6304) at android.view.View$PerformClick.run(View.java:24803) at android.os.Handler.handleCallback(Handler.java:794) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:6651) at java.lang.reflect.Method.invoke(Native Method) at Z4D236D9A
und.widget.BaseItem.performClickSelf(BaseItem.java:6) at com.android.settings.coolsound.widget.BaseItem.onClick(BaseItem.java:1) at android.view.View.performClick(View.java:6304) at android.view.View$PerformClick.run(View.java:24803) at android.os.Handler.handleCallback(Handler.java:794) at android.os.Handler.dispatchMessage(Handler.java:99) at android. os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:6651) at java.lang.reflect.Method.invoke(Native Method) at Z4D236D9A 2D102C5FE6AD1C50DA4BEC50Z.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
2D102C5FE6AD1C50DA4BEC50Z.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.