Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit Intent to view URL

Tags:

android

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.

like image 766
user3284769 Avatar asked Feb 07 '14 19:02

user3284769


2 Answers

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));
like image 76
2Dee Avatar answered Sep 28 '22 04:09

2Dee


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 );
like image 36
Aniket Thakur Avatar answered Sep 28 '22 04:09

Aniket Thakur