5

I am new to Android, I want to create an Intent to view google website. My String is declared as follows:

static private final String URL = "http://www.google.com";

and my Intent :

Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setData(Uri.parse("geo:+URL"));
startActivity(browserIntent);

This code shows no errors in Eclipse, but I think it might be wrong.

2Dee
  • 8,510
  • 7
  • 40
  • 53
user3284769
  • 63
  • 1
  • 2
  • 5

4 Answers4

6

You're not building your Uri correctly, when trying to concatenate 2 String, use this :

String s = "I'm a string variable";

String concatenated = s + " and I'm another String variable";

Now the content of concatenated is

I'm a string variable and I'm another String variable

If you do this :

String concatenated = "s + and I'm another String variable";

the content of concatenated is

s + and I'm another String variable

Secondly, why are you using a geo Uri ? This is for viewing locations. To view a website, just use the URL (and don't forget the "http://" part) :

String URL = "http://www.google.com";
browserIntent.setData(Uri.parse(URL));
2Dee
  • 8,510
  • 7
  • 40
  • 53
  • No, you should not leave out the http part, or will end up with an error, use browserIntent.setData(Uri.parse("http://google.com")); Have a look at this question for details : http://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application?rq=1 – 2Dee Feb 07 '14 at 19:52
  • browserIntent.setData(Uri.parse(URL)); i've put it like this – user3284769 Feb 07 '14 at 19:55
  • Mmmh, looks like SO comments system is hiding the http part, sorry, I didn't know about that, just noticed mine was cut as well :) Using Uri.parse(URL) will work just fine. – 2Dee Feb 07 '14 at 19:59
2

You can create an intent and then set data for it. Intent also has a constructor that takes action String and data URI.

Also you need to use geo: when you want to show something on a map. So view an URL in browser you can simply use the website URL. You can pass it to Uri.parse() method to get URI object needed in the intents constructor. You can simply do -

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
Intent browserChooserIntent = Intent.createChooser(browserIntent , "Choose browser of your choice");
startActivity(browserChooserIntent );
Aniket Thakur
  • 58,991
  • 35
  • 252
  • 267
0

2Dee's answer is correct. You can also give the action and set data uri in a single line. To view the website.

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
startActivity(browserIntent);
sutro
  • 613
  • 6
  • 13
0
 Intent browserIntent=null, chooser=null;
    browserIntent= new Intent(Intent.ACTION_VIEW);
    browserIntent.setData(Uri.parse("http://www.google.com"));
    chooser = browserIntent.createChooser(intent,"Open Website Using...");
    if(browserIntent.resolveActivity(getPackageManager()) != null){
        startActivity(chooser);
    }