1

How can I open a url in the android web browser from my application on clicking a button.

I tried this :

Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));

startActivity(myIntent);

but I am getting exception:

but I got an Exception :

 "No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com"
Graham Smith
  • 25,627
  • 10
  • 46
  • 69
sanya
  • 41
  • 5

4 Answers4

2

You are doing it fundamentally correct, you just need to include the full url.

String strURL="http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);

Basically, the http is the protocol. It is the computer's way of trying to figure out how you want to open it. You might also be interested in https, if you want a secure connection over SSL.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
1

Almost seems like this would be useful to read... from the page:

Uri uri = Uri.parse("http://androidbook.blogspot.com");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uri);
startActivity(launchBrowser);

There is a second way, involving the WebView, but that seems like it's not your question. Hope this helped.

hd1
  • 33,938
  • 5
  • 80
  • 91
0
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
al3x1s
  • 70
  • 1
  • 9
  • 1
    Your answer would be considerably better if you could explain _why_ you think this answers the question. – Ben Nov 21 '12 at 11:54
0

Try this:

String strURL = "http://www.google.com";
        Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));

        startActivity(myIntent);

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

if (!url.startsWith("http://") && !url.startsWith("https://"))
   url = "http://" + url;
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Arvind Kanjariya
  • 2,089
  • 1
  • 18
  • 23