ChatGPT解决这个技术问题 Extra ChatGPT

Sending an Intent to browser to open specific URL [duplicate]

This question already has answers here: How can I open a URL in Android's web browser from my application? (41 answers) Closed 8 years ago.

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?


a
aioobe

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


In production level code, you may like to check if the url begins with http or https... Would be better to check if (!url.startsWith("http://") && !url.startsWith("https://")) url = "http://" + url;
Encode the Query String If any special characters or spaces. then It will work awesome.For Example : String query="For martin Luther King"; query=URLEncoder.encode(query); String url="en.wikipedia.org/wiki/Special:Search?search="+query; Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browserIntent);
Funny thing here is that startActivity(i) line could produce ActivityNotFound exception so I go for wrap this line in try/catch block to prevent app crash. This could happen if really no browser app installed on target device (yeah, shoot happens) also it could be that your app was forbidden to start a browser using restrict profiles.
From Android Developer web site: Caution: If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity(). To first verify that an app exists to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.
@MahendraLiya The question was about sending a Intent to open the Browser, not about parsing the URL. If the URL is hardcoded, you wouldn't check for http(s), would you? So your argument to invalue the answer is simply invalid, and i do not understand why it has so many upvotes. Funniest thing is, no answer provided states that startActivity can only be called inside certain places, if someone wants to know that, he has to look it up in the docs - but if he wants to parse the URL, he just has to look at the comments.
n
norbDEV

The short version

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

should work as well...


t
tomrozb

The shortest version.

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

T
TouchBoarder

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

I went with Matcher _SCHEMA_MATCHER = Pattern.compile("(https?://|mailto:).+").matcher("") and return _SCHEMA_MATCHER.reset(uri).matches()? uri : "http://" + uri.
ActivityNotFoundException you can handle by keeping try catch. in catch block you keep toast message to get remote user clarity.
J
Jorgesys

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

=)


What if the uri is missing http?
P
Phil

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

S
Saty

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


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

X
XYZ_deve

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


C
Community

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