views:

1886

answers:

1

How to open an URL from code in the built-in web browser rather than within my application ?

I tried this : Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL)); startActivity(myIntent);

but I got an Exception : "No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com"

+9  A: 

Try this:

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com"));
startActivity(browserIntent);

That works fine for me.

As for the missing "http://" I'd just do something like this:

if (!url.startsWith("http://") && !url.startsWith("https://"))
   url = "http://" + url;

I would also probably pre-populate your EditText that the user is typing a URL in with "http://".

mbaird
and not for me...
Arutha
Except that your code and mbaird's aren't the same, from what I can tell for what's posted. Ensure that your URL has the `http://` scheme -- the exception shown suggests that your URL is lacking the scheme.
CommonsWare
Yes ! It missed the http:// !The URL is entered by the user, is there a way to automatically format?
Arutha
See the update to my answer above regarding detecting the missing http://
mbaird
Tried this Intent and it works great!
pcm2a
thank you all !
Arutha
It needed another ')' after '("http://www.google.com")', but other than that it worked. Thanks :-)
Techboy