Launch Browser Intent with Custom Class - cannot find Activity

the_void picture the_void · Jul 25, 2010 · Viewed 11.4k times · Source

I want to specifically run the default Android browser for a given URL. I'm using this code:

Intent i = new Intent();
i.setAction("android.intent.action.VIEW"); 
i.addCategory("android.intent.category.BROWSABLE");
i.setClassName("com.google.android.browser", "com.android.browser.BrowserActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
startActivity(i);

The error I receive is:

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

I also tried filtering the intents by the package:

i.setPackage("com.google.android.browser");

instead of setClassName, but to no avail:

No Activity found to handle Intent { act=android.intent.action.VIEW 
cat=[android.intent.category.BROWSABLE] 
dat=http://www.google.com/ flg=0x10000000 pkg=android }

I also tried adding <uses-library android:name="com.google.android.browser" /> to the manifest.

Am I missing something here?

PS: I'm not interested in using startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))) as it will list all the choices for the browsing Intent.

Answer

Pentium10 picture Pentium10 · Jul 25, 2010

Please note the default browser can be overridden and it's not always the built in browser app, it can be for example Opera Mini.

You need to do this way:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("http://www.google.com");
intent.setData(data);
startActivity(intent);