致命异常:android.content.ActivityNotFoundException:没有找到处理意图的活动

问题描述

我在某些设备上打开 url 时遇到以下错误。我的设备没有出现任何错误,但很多设备都有。

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)

发生错误代码非常简单。

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
}

在某些设备(不是旧设备,主要是最新设备)中,会触发异常,但怎么可能呢?这意味着设备有 0 个浏览器?如果是这样,我的问题是如何即使用户禁用所有浏览器也能打开 url?提前致谢。

注意:在我升级到实时版本中的自定义标签之前,我使用以下代码打开网址,但我总是得到ActivityNotFoundException

Intent browserIntent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(browserIntent);

这是最简单的代码,打开任何 url 但不使用 try/catch 会导致崩溃。使用普通方式或自定义选项卡都没有关系,它总是导致 ActivityNotFoundException。我正在寻找一种解决方案,无论发生什么,都可以打开 url。我不确定这是否可能。

注意 2:MinApi 是 Android 5.0 (Lollipop)

enter image description here

解决方法

关于这个问题的一些说明:

将非浏览器应用检测为浏览器

他们查询浏览器可以检测非浏览器应用程序,这将在启动自定义选项卡时引导 ActivityNotFoundException。有关示例,请参阅 this issue

在查询可以处理浏览器意图的包时,我建议使用以下内容:


Intent browserIntent = new Intent()
        .setAction(Intent.ACTION_VIEW)
        .addCategory(Intent.CATEGORY_BROWSABLE)
        .setData(Uri.fromParts("http","",null));

这也是Android framework itself detects browsers的方式。

以下应该不是问题,因为自定义选项卡仍然可以工作,但是 PackageManager.MATCH_DEFAULT_ONLY 的行为在 Android M 上发生了变化并且只返回默认浏览器 - 我不确定如果未设置默认浏览器,但这可能意味着返回的列表将为空。这意味着当未设置默认浏览器时,用户将获得消歧对话框。在 Android M 及更高版本中,如果您想让所有浏览器避免出现歧义对话框,您可能还需要使用 PackageManager.MATCH_ALL 额外查询包管理器。

查看 android-browser-helper 库中的 this snippet 以检测自定义标签和受信任的 Web 活动。

为 Android 11 做准备

Android 11 引入了 package visibility,应用程序现在必须声明它们要查询的包。如果您的应用程序未实现此功能,即使安装了浏览器,查询包管理时返回的列表也将为空。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    possibleProviders.addAll(pm.queryIntentActivities(queryBrowsersIntent,PackageManager.MATCH_ALL));
}

同样,这不应导致 ActivityNotFoundException,因为它只会导致自定义选项卡在没有设置包的情况下启动。

已禁用浏览器

注意:在我升级到实时版本中的自定义选项卡之前,我使用以下代码打开 url 但我总是得到 ActivityNotFoundException:

Intent browserIntent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(browserIntent);

用户可以禁用所有浏览器应用程序,但我不希望这种情况很常见。

另一件需要注意的事情是 Android TV 和 Android Auto 设备没有安装浏览器。如果您的应用程序针对这些设备,您将无法打开这些 URL。但这只有在您的应用面向这些平台时才会出现问题。

,

我创建了一个基于@andreban 的回答的方法。我相信它会打开 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);
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...