Android 向浏览器发送 Intent 以打开特定 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3004515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:14:17  来源:igfitidea点击:

Sending an Intent to browser to open specific URL

androidandroid-intent

提问by poeschlorn

I'm just wondering how to fire up an Intent to the phone's browser to open an specific URL and display it.

我只是想知道如何向手机的浏览器启动 Intent 以打开特定的 URL 并显示它。

Can someone please give me a hint?

有人可以给我一个提示吗?

回答by aioobe

To open a URL/website you do the following:

要打开 URL/网站,请执行以下操作:

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.

Intent.ACTION_VIEW.



Source: Opening a URL in Android's web browser from within application

来源:从应用程序内在 Android 的网络浏览器中打开 URL

回答by Juri

The short version

简短的版本

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

should work as well...

也应该工作......

回答by Gianluca

The shortest version.

最短的版本。

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

回答by Bakyt

In some cases URL may start with "www". In this case you will get an exception:

在某些情况下,URL 可能以“www”开头。在这种情况下,你会得到一个例外:

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:

URL 必须始终以“http://”或“https://”开头,因此我使用以下代码片段:

if (!url.startsWith("https://") && !url.startsWith("http://")){
    url = "http://" + url;
}
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(openUrlIntent);

回答by Jorgesys

Sending an Intent to Browser to open specific URL:

向浏览器发送 Intent 以打开特定 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

有关意图的更多信息

=)

=)

回答by Phil

Is there also a way to pass coords directly to google maps to display?

还有一种方法可以将坐标直接传递给谷歌地图进行显示吗?

You can use the geoURIprefix:

您可以使用地理URI前缀:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:" + latitude + "," + longitude));
startActivity(intent);

回答by Saty

From XML

来自 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:

如果您的视图中显示了网址/URL,并且您希望它使其可点击并将用户定向到特定网站,您可以使用:

android:autoLink="web"

In same way you can use different attributes of autoLink(email, phone, map, all) to accomplish your task...

以同样的方式,您可以使用自动链接的不同属性(电子邮件、电话、地图、所有)来完成您的任务......

回答by Sunil Pandey

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

回答by 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

http://developer.android.com/reference/android/content/Intent.html#ACTION_VIEW

回答by Kingsolmn

"Is there also a way to pass coords directly to google maps to display?"

“还有没有办法直接把坐标传到google地图上显示?”

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 herefor more info on the formating of the URL.

我发现如果我将包含坐标的 URL 传递给浏览器,只要用户没有选择浏览器作为默认浏览器,Android 就会询问我是想要浏览器还是地图应用程序。见我的答案在这里为在URL的格式化的更多信息。

I guess if you used an intent to launch the Maps App with the coords, that would work also.

我想如果您使用意图通过坐标启动地图应用程序,那也可以。