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

有人能给我一个提示吗?


当前回答

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

其他回答

在某些情况下,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 browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

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

您可以使用地理URI前缀:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:" + latitude + "," + longitude));
startActivity(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); 

or

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的更多信息

=)

在代码中使用以下代码段

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