9

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.

the_void
  • 5,512
  • 2
  • 28
  • 34

3 Answers3

6

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);
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • 4
    As I've stated in my question, I want to be able to run the *default Android browser*, not the *default selected* browser. As a side node, I've first tried with the solution you mention, but to no avail as it obviously launches the browser that is set as default (or launches the application selector). – the_void Jul 25 '10 at 15:15
  • how to use this intent in a non activity class?? – Maxwell May 03 '14 at 06:36
  • The drawback for this - for local html files it launches HTMLViewer app instead of browser. – JustAMartin Feb 20 '17 at 16:55
6

i use this, it's ok.

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

i think you know what wrong. :)

Janusz
  • 187,060
  • 113
  • 301
  • 369
yuni
  • 76
  • 2
1

One way to open an URL in a browser from code is to use a webview .

Create a WebViewClientClass that extends the WebViewClient like :

public boolean shouldOverrideUrlLoading(WebView view, String url) {
   view.loadUrl(url);
   return true;
}

Then create a webview. Set webview.setWebViewClient(new WebViewClientClass()); --- this is a small workaround so that the default web browser doesn't take over .

Then take the url in an edittext and set it to load the browser as :

webview.loadurl(urlfield.getText().toString());
webview.requestFocus();

This should load the web browser with the URL that you requested.

Hope this helps... :)

Janusz
  • 187,060
  • 113
  • 301
  • 369
Flash
  • 2,901
  • 5
  • 38
  • 55
  • do not set the webview.setWebViewClient if you want your default web browser to take over your request ... – Flash Jul 26 '10 at 17:16
  • 1
    I don't want to launch a browser window (or for that matter use *my own* browser). I want to launch the *default browser that comes with Android*. Also, launching a browser window would introduce additional complexity, like managing the cache, bookmarks, back button, etc, etc. Btw, please format your code ;) – the_void Jul 26 '10 at 18:23