繁体   English   中英

致命异常:android.content.ActivityNotFoundException:未找到处理 Intent 的活动

[英]Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent

打开 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 即使用户禁用了所有浏览器 提前致谢。

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

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

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

注2: MinApi 为 Android 5.0 (Lollipop)

在此处输入图像描述

关于这个问题的几点说明:

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

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

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


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

这也是Android 框架本身检测浏览器的方式。

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

查看android-browser-helper库中的此代码段,以检测自定义选项卡和受信任的 Web 活动。

准备 Android 11

Android 11 引入了package 可见性和应用程序现在必须声明它们正在查询哪些包。 如果您的应用程序没有实现此功能,则在查询 package 管理时返回的列表将为空,即使安装了浏览器也是如此。

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

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

禁用的浏览器

注意:在实时版本中升级到自定义选项卡之前,我使用以下代码打开 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);
    }
}

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 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM