Sending an Intent to browser to open specific URL

2018-12-31 12:33发布

问题:

This question already has an answer here:

  • How can I open a URL in Android's web browser from my application? 29 answers

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?

回答1:

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



回答2:

The short version

Intent i = new Intent(Intent.ACTION_VIEW, 
       Uri.parse(\"http://almondmendoza.com/android-applications/\"));
startActivity(i);

should work as well...



回答3:

The shortest version.

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(\"http://www.google.com\")));


回答4:

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);


回答5:

Sending an Intent to Browser to open specific URL:

String url = \"http://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

=)



回答6:

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);


回答7:

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...



回答8:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(\"http://www.google.com\"));
startActivity(browserIntent);


回答9:

\"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.



回答10:

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