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

前端 未结 5 1031
忘了有多久
忘了有多久 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: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 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);
        }
    }
    

提交回复
热议问题