I'm just wondering how to fire up an Intent to the phone's browser to open an specific URL and display it.
Can someone please give me a hint?
To open a URL/website you do the following:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Here's the documentation of Intent.ACTION_VIEW
.
Source: Opening a URL in Android's web browser from within application
The short version
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://almondmendoza.com/android-applications/")));
should work as well...
The shortest version.
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
In some cases URL may start with "www". In this case you will get an exception:
android.content.ActivityNotFoundException: No Activity found to handle Intent
The URL must always start with "http://" or "https://" so I use this snipped of code:
if (!url.startsWith("https://") && !url.startsWith("http://")){
url = "http://" + url;
}
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(openUrlIntent);
Sending an Intent to Browser to open specific URL:
String url = "https://www.stackoverflow.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
could be changed to a short code version ...
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com"));
startActivity(intent);
or
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com"));
startActivity(intent);
or even more short!
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
More info about Intent
=)
Is there also a way to pass coords directly to google maps to display?
You can use the geo URI
prefix:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:" + latitude + "," + longitude));
startActivity(intent);
From XML
In case if you have the web-address/URL displayed on your view and you want it to make it clikable and direct user to particular website You can use:
android:autoLink="web"
In same way you can use different attributes of autoLink(email, phone, map, all) to accomplish your task...
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
Use following snippet in your code
Intent newIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.google.co.in/?gws_rd=cr"));
startActivity(newIntent);
Use This link
http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW
"Is there also a way to pass coords directly to google maps to display?"
I have found that if I pass a URL containing the coords to the browser, Android asks if I want the browser or the Maps app, as long as the user hasn't chosen the browser as the default. See my answer here for more info on the formating of the URL.
I guess if you used an intent to launch the Maps App with the coords, that would work also.
精彩评论