繁体   English   中英

向浏览器发送意图以打开特定 URL [重复]

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

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

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

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

String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

Intent.ACTION_VIEW文档


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

简短的版本

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

也应该工作......

最短的版本。

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

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

android.content.ActivityNotFoundException: No Activity found to handle Intent

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

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

String url = "https://www.stackoverflow.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url)); 
startActivity(i); 

可以更改为短代码版本...

Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com"));      
startActivity(intent); 

或者

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

甚至更短!

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

有关意图的更多信息

=)

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

您可以使用地理URI前缀:

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

来自 XML

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

android:autoLink="web"

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

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

在您的代码中使用以下代码段

Intent newIntent = new Intent(Intent.ACTION_VIEW, 
Uri.parse("https://www.google.co.in/?gws_rd=cr"));
startActivity(newIntent);

使用此链接

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM