ChatGPT解决这个技术问题 Extra ChatGPT

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

这个问题在这里已经有了答案:如何从我的应用程序中打开 Android 的网络浏览器中的 URL? (41 个回答) 8 年前关闭。

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

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


a
aioobe

要打开 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


在生产级代码中,您可能想检查 url 是否以 httphttps 开头...最好检查 if (!url.startsWith("http://") && !url.startsWith("https://")) url = "http://" + url;
如果有任何特殊字符或空格,则对查询字符串进行编码。那么它会很好用。例如:String query="For martin Luther King";查询=URLEncoder.encode(查询); String url="en.wikipedia.org/wiki/Special:Search?search="+query; Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browserIntent);
有趣的是 startActivity(i) 行可能会产生 ActivityNotFound 异常,所以我将这一行包装在 try/catch 块中以防止应用程序崩溃。如果目标设备上确实没有安装浏览器应用程序(是的,发生了拍摄),则可能会发生这种情况,也可能是您的应用程序被禁止使用限制配置文件启动浏览器。
来自 Android 开发者网站: 注意:如果设备上没有可以接收隐式 Intent 的应用程序,您的应用程序将在调用 startActivity() 时崩溃。要首先验证应用是否存在以接收 Intent,请在 Intent 对象上调用 resolveActivity()。如果结果为非空,则至少有一个应用程序可以处理该意图,并且调用 startActivity() 是安全的。如果结果为 null,则不应使用该意图,并且如果可能,您应该禁用调用该意图的功能。
@MahendraLiya问题是关于发送打开浏览器的意图,而不是解析URL。如果 URL 是硬编码的,您就不会检查 http(s),对吗?因此,您认为答案价值不高的论点根本无效,我不明白为什么它有这么多赞成票。最有趣的是,没有提供答案说明 startActivity 只能在某些地方调用,如果有人想知道,他必须在文档中查找它 - 但如果他想解析 URL,他只需要查看评论。
n
norbDEV

短版

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

应该也可以...


t
tomrozb

最短的版本。

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

T
TouchBoarder

在某些情况下,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);

我选择了 Matcher _SCHEMA_MATCHER = Pattern.compile("(https?://|mailto:).+").matcher("") 并返回 _SCHEMA_MATCHER.reset(uri).matches()? uri : "http://" + uri
ActivityNotFoundException 你可以通过保持 try catch 来处理。在 catch 块中,您保留 toast 消息以使远程用户清晰。
J
Jorgesys

向浏览器发送 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")));

关于 Intent 的更多信息

=)


如果 uri 缺少 http 怎么办?
P
Phil

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

您可以使用 geo URI 前缀:

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

S
Saty

从 XML

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

android:autoLink="web"

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


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

X
XYZ_deve

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

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


C
Community

“还有没有办法将坐标直接传递给谷歌地图显示?”

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

我想如果你使用一个意图来启动带有坐标的地图应用程序,那也可以。