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

时间:2010-06-09 09:21:58

标签: android android-intent

我只是想知道如何在手机的浏览器中启动一个Intent来打开一个特定的URL并显示它。

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

10 个答案:

答案 0 :(得分:1626)

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

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

答案 1 :(得分:179)

简短版

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

也应该有用......

答案 2 :(得分:148)

最短的版本。

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

答案 3 :(得分:81)

在某些情况下,网址可能以“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);

答案 4 :(得分:40)

将意图发送到浏览器以打开特定URL:

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

的详细信息

=)

答案 5 :(得分:27)

  

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

您可以使用 geo URI前缀:

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

答案 6 :(得分:12)

来自XML

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

android:autoLink="web"

同样,您可以使用autoLink的不同属性(电子邮件,电话,地图,全部)来完成您的任务......

答案 7 :(得分:10)

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

答案 8 :(得分:7)

在代码中使用以下代码段

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

答案 9 :(得分:6)

  

“还有一种方法可以将coords直接传递给谷歌地图显示吗?”

我发现如果我将包含coords的URL传递给浏览器,只要用户没有选择浏览器作为默认设置,Android就会询问我是否需要浏览器或Google地图应用。有关格式化URL的详细信息,请参阅我的回答here

我想如果您使用意图使用coords启动Maps App,那也可以。