opening-local-html-file-with-android-browser in an

2019-05-07 12:42发布

with android 2.x i could use the solution

browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity") 

resolved in the post:

opening local html file with Android Browser

but with android 3.2 i have this error:

Unable to find explicit activity class 
(com.android.browser/com.android.browser.BrowserActivity); 
have you declared this activity in your AndroidManifest.xml?

I think that the class com.android.browser.BrowserActivity doesn't exist in Android 3.x

Any solution?

3条回答
戒情不戒烟
2楼-- · 2019-05-07 13:03
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity")

Will open whatever app has the packagename "com.android.browser". The issue with using this is that over various versions of android and various manufacturers, the default browser changes. For example Nexus devices tend to come pre-installed with the chrome app, which has a different package name.

Unable to find explicit activity class 
(com.android.browser/com.android.browser.BrowserActivity); 
have you declared this activity in your AndroidManifest.xml?

The error you have copied explains that there is no application with that package name browserIntent.setClassName() is used to open a specific app explicitly which means it shouldn't provide a prompt asking which browser you would like to use.

If this is what you want to avoid (pop up), then you can check what browsers are on the device and possibly suggest downloading it before making links clickable.

you also could use the code from the other suggestion.

String url = "content://com.android.htmlfileprovider/sdcard/mydir/myfile.html";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "text/html");
startActivity(intent);

This specifies that you want to open an activity that can handle the "text/html" type data. By viewing it. This would provide you with a list of applications (different installed browsers) for you to select.

查看更多
男人必须洒脱
3楼-- · 2019-05-07 13:17

Not a satisfying solution, but in a demo application I alternatively used the following code:

String url = "content://com.android.htmlfileprovider/sdcard/mydir/myfile.html";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "text/html");
startActivity(intent);

Usually at least one of the two works.

https://github.com/nicolas-raoul/OxygenGuide-Android/blob/master/src/org/github/OxygenGuide/MainActivity.java#L69

查看更多
Bombasti
4楼-- · 2019-05-07 13:20
Uri uri = Uri.parse(url);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setDataAndType(uri, "text/html");
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(browserIntent);
查看更多
登录 后发表回答