Android: Activity not found exception on some devices, when trying to open local HTML - file in browser

前端 未结 5 999
忘了有多久
忘了有多久 2020-12-29 12:51

I\'m encountering a strange error when I try to open a local HTML - file in the android browser. The error that occurs is an activity not found exception:

an         


        
相关标签:
5条回答
  • 2020-12-29 13:11

    Why are you specifying a component? leave it outside of your Intent and you should be fine; the action, category and data/type are enough.

    0 讨论(0)
  • 2020-12-29 13:22

    New devices can come with Chrome instead of native browser, so you should use for this

    browserIntent.setComponent(new ComponentName("com.android.chrome", "com.google.android.apps.chrome.IntentDispatcher"));

    0 讨论(0)
  • 2020-12-29 13:24

    Possible because may be there is no any activity like com.android.browser.BrowserActivity in those devices, Its depends on device manufacturer How they implement the native Browser Application (Activity Name and Package name).

    So the possible solution is,

    Using PackageManger and Intent you can check for specific intent category like, Intent.CATEGORY_BROWSABLE is available for any application if available then set that application to ComponentName.

    Or, You don't specify component name, like,

    browserIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
    

    let user have to choose, which Activity will open this page,

    So just code is,

    Uri uri = Uri.parse(filePath);
    Intent browserIntent = new Intent(Intent.ACTION_VIEW);
    browserIntent.setDataAndType(uri, "text/html");
    browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
    context.startActivity(browserIntent);
    
    0 讨论(0)
  • 2020-12-29 13:28

    Why not leave it to the system to determine which app to use? If I have (for example) Chrome installed and perfer to use Chrome, I'll be a bit pissed that you forced the default browser on me.

    This should work:

    final Uri uri = Uri.parse(filePath);
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
    browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
    context.startActivity(browserIntent);
    
    0 讨论(0)
  • 2020-12-29 13:36

    in some scenario you might want to use the native browser instead of letting user chose one, this is how I did to avoid ActivityNotFoundException by different package name, hope this would help :)

    Intent browserIntent = new Intent(Intent.ACTION_VIEW);
    PackageManager packageManager = currentActivity.getPackageManager();
    Uri uri = Uri.parse(url);
    browserIntent.setDataAndType(uri, "text/html");
    List<ResolveInfo> list = packageManager.queryIntentActivities(browserIntent, 0);
    for (ResolveInfo resolveInfo : list) {
        String activityName = resolveInfo.activityInfo.name;
        if (activityName.contains("BrowserActivity")) {
            browserIntent =
                    packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName);
            ComponentName comp =
                    new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
            browserIntent.setAction(Intent.ACTION_VIEW);
            browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            browserIntent.setComponent(comp);
            browserIntent.setData(uri);
        }
    }
    
    0 讨论(0)
提交回复
热议问题