10

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:

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's really strange to me ist the fact that this error does not occur on all devices. It only comes up 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 tested my Code successfully 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
  • AVDs with Android Versions ranging from 2.3.3 to 4.1.0

Finally this is the Code I'm using to open my HTML - file with the Android - browser. I have tested several permutations of this code, leaving out some lines, but they do all have the same effect. As soon as I set component or class I getting the above exception.

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 have also tried

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

instead of

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

But with the same effect...

Has someone an idea why this is happening, am I missing something? I have searched for days on this but couldn't find anything that solved my problem...

Thanks a lot in advance, cheers

Henning
  • 101
  • 1
  • 4
  • I found same error with Intent.ACTION_SENDTO and trying to create a chooser with startActivity(Intent.createChooser()) on Samsung Galaxy Nexus I (4.1.1). Haven't found a solution. – ThanksMister Apr 03 '14 at 14:54

5 Answers5

14

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);
user370305
  • 108,599
  • 23
  • 164
  • 151
  • But isn't com.android.browser.BrowserActivity THE standard Activity for the browser? So it should be implemented by Google... I tested your code for giving the user a choice is leading me to the following exception: BrowserHelper(13717): Error starting Browser Activity: No Activity found to handle Intent – Henning Aug 01 '12 at 09:01
  • Device Manufacturer can modified the Source Code of **Browser Application** and implement as per their requirement to add features.. **BrowserActivity** is only for Google Source code. – user370305 Aug 01 '12 at 09:04
  • @Henning, not necessarily. Android is an open source project. The manufacturer could go on his own way. – Andy Res Aug 01 '12 at 09:04
  • I see what you mean... but the Nexus Smartphones are the Google smartphones, aren't they? So I would expect the code to work on theses devices... – Henning Aug 01 '12 at 09:13
  • You can check what is activity name for Browser Application, either by logcat when you launch browser application from device or Using Package Manger for specific Intent category.. – user370305 Aug 01 '12 at 09:15
7

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);
    }
}
Mia
  • 1,226
  • 1
  • 19
  • 29
  • Its good way to find and open the url from the any existing browser in the mobile(**If packege and class name is known**) and also avoid the **ActivityNotFoundException Exception**. – amardeep Dec 26 '14 at 13:49
4

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);
copolii
  • 14,208
  • 10
  • 51
  • 80
1

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.

avimak
  • 1,628
  • 11
  • 18
  • not specifying the component or class always leads me to an exception telling me the system could not find an acivity to handle my intent – Henning Aug 01 '12 at 09:05
0

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"));

B-GangsteR
  • 2,534
  • 22
  • 34
  • You can uninstall Chrome though... Don't specify a specific component but let the system choose for you – fm-sys Dec 02 '21 at 15:18