Android: action not detected on some devices when trying to open local HTML file in browser

I get a strange error when I try to open a local HTML file in an Android browser. An error occurred: an exception not found in the activity:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.browser/com.android.browser.BrowserActivity}; have you declared this activity in your AndroidManifest.xml? 

What is really strange for me is that this error does not occur on all devices. It appears only on the following devices:

  • Samsung Galaxy Nexus i (4.1.1)
  • Samsung Galaxy Nexus II (4.1.1)
  • Sony Tablet S (4.0.3)

I have successfully verified my code on the following devices:

  • Samsung Galaxy S i (4.1.1)
  • Samsung Galaxy S III
  • HTC One X
  • HTC Desire S
  • Samsung Galaxy S Plus (2.3.3)
  • Samsung Galaxy Tab 10.1N
  • AVD with Android versions 2.3.3 to 4.1.0

Finally, this is the code that I use to open my HTML file with an Android browser. I tested several permutations of this code, leaving some lines, but they all have the same effect. As soon as I install the component or class, I get the exception above.

 Uri uri = Uri.parse(filePath); Intent browserIntent = new Intent(Intent.ACTION_VIEW); browserIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity")); browserIntent.setDataAndType(uri, "text/html"); browserIntent.addCategory(Intent.CATEGORY_BROWSABLE); context.startActivity(browserIntent); 

I also tried

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

instead

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

But with the same effect ...

Does anyone have an idea why this is happening, am I missing something? I searched for days on this, but could not find anything that solved my problem ...

Thank you very much in advance, greetings

+8
android browser android-activity exception
source share
4 answers

Perhaps because these devices do not have any activity like com.android.browser.BrowserActivity . Depends on the device manufacturer. . How they implement their own browser application / strong> (Operation name and package name).

So, a possible solution:

Using PackageManger and Intent , you can check the category of a specific intent, for example, Intent.CATEGORY_BROWSABLE available for any application, if available, then set this application to ComponentName.

Or, you do not specify the name of the component, for example,

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

let the user choose which activity will open this page,

So, just a code,

 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); 
+14
source share

in some scenarios you can use your own browser instead of letting the user select one, this is how I did to avoid an ActivityNotFoundException for a 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); } } 
+7
source share

Why not leave it on the system to determine which application to use? If I have (for example) Chrome installed and perfer to use Chrome, I will be a little angry that you made me use the default browser.

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); 
+4
source share

Why are you specifying a component? leave it out of your intention, and you should be in order; action, category and data type / enough.

+1
source share

All Articles