【发布时间】:2011-03-01 13:50:34
【问题描述】:
我只是想知道如何向手机的浏览器启动一个 Intent 以打开一个特定的 URL 并显示它。
有人可以给我一个提示吗?
【问题讨论】:
我只是想知道如何向手机的浏览器启动一个 Intent 以打开一个特定的 URL 并显示它。
有人可以给我一个提示吗?
【问题讨论】:
短版
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://almondmendoza.com/android-applications/")));
应该也可以...
【讨论】:
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")));
关于Intent
的更多信息=)
【讨论】:
要打开 URL/网站,请执行以下操作:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
这是documentation of Intent.ACTION_VIEW。
来源:Opening a URL in Android's web browser from within application
【讨论】:
http或https开头...最好检查if (!url.startsWith("http://") && !url.startsWith("https://")) url = "http://" + url;
如果您的视图中显示了网址/URL,并且您希望它使其可点击并将用户引导至特定网站,您可以使用:
android:autoLink="web"
以同样的方式,您可以使用 autoLink 的不同属性(电子邮件、电话、地图、全部)来完成您的任务...
【讨论】:
最短的版本。
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
【讨论】:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
【讨论】:
在您的代码中使用以下 sn-p
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
【讨论】:
在某些情况下,URL 可能以“www”开头。在这种情况下你会得到一个异常:
android.content.ActivityNotFoundException: No Activity found to handle Intent
网址必须始终以“http://”或“https://”开头,所以我使用这段代码:
if (!url.startsWith("https://") && !url.startsWith("http://")){
url = "http://" + url;
}
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(openUrlIntent);
【讨论】:
Matcher _SCHEMA_MATCHER = Pattern.compile("(https?://|mailto:).+").matcher(""),然后返回_SCHEMA_MATCHER.reset(uri).matches()? uri : "http://" + uri。
还有没有办法将坐标直接传递给谷歌地图显示?
您可以使用 geo URI 前缀:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:" + latitude + "," + longitude));
startActivity(intent);
【讨论】:
“还有没有办法将坐标直接传递给谷歌地图显示?”
我发现如果我将包含坐标的 URL 传递给浏览器,只要用户没有选择浏览器作为默认浏览器,Android 就会询问我是想要浏览器还是地图应用程序。有关 URL 格式的更多信息,请参阅我的回答 here。
我想如果你使用一个意图来启动带有坐标的地图应用程序,那也可以。
【讨论】: