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

poe*_*orn 713 android android-intent

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

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

aio*_*obe 1679

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

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

这是文档Intent.ACTION_VIEW.


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

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

Jur*_*uri 183

简短的版本

__PRE__

也应该工作......


小智 153

最短的版本.

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
Run Code Online (Sandbox Code Playgroud)


Bak*_*kyt 83

在某些情况下,URL可能以"www"开头.在这种情况下,您将获得一个例外:

android.content.ActivityNotFoundException: No Activity found to handle Intent
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)


Jor*_*sys 44

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

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

可以改成短代码版本......

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

要么

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

甚至更短!

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
Run Code Online (Sandbox Code Playgroud)

有关Intent的更多信息

=)


Phi*_*hil 26

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

您可以使用地理 URI前缀:

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


Sat*_*aty 13

来自XML

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

android:autoLink="web"
Run Code Online (Sandbox Code Playgroud)

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


小智 10

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


小智 7

在代码中使用以下代码段

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

使用此链接

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


Kin*_*lmn 6

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

我发现如果我将包含coords的URL传递给浏览器,只要用户没有选择浏览器作为默认设置,Android就会询问我是否需要浏览器或Google地图应用.见我的答案在这里为在URL的格式化的更多信息.

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