【问题标题】:Sending an Intent to browser to open specific URL [duplicate]向浏览器发送意图以打开特定 URL [重复]
【发布时间】:2011-03-01 13:50:34
【问题描述】:

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

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

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    短版

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

    应该也可以...

    【讨论】:

      【解决方案2】:

      向浏览器发送 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 怎么办?
      【解决方案3】:

      要打开 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 问题是关于发送 Intent 以打开浏览器,而不是解析 URL。如果 URL 是硬编码的,您就不会检查 http(s),对吗?因此,您认为答案价值不高的论点根本无效,我不明白为什么它有这么多赞成票。最有趣的是,没有提供答案说明 startActivity 只能在某些地方调用,如果有人想知道,他必须在文档中查找它 - 但如果他想解析 URL,他只需要查看cmets。
      【解决方案4】:

      来自 XML

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

      android:autoLink="web"
      

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

      【讨论】:

        【解决方案5】:

        最短的版本。

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

        【讨论】:

          【解决方案6】:
          Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
          startActivity(browserIntent);
          

          【讨论】:

            【解决方案7】:

            在您的代码中使用以下 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

            【讨论】:

              【解决方案8】:

              在某些情况下,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
              • ActivityNotFoundException 您可以通过保持 try catch 来处理。在 catch 块中,您保留 toast 消息以使远程用户清晰。
              【解决方案9】:

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

              您可以使用 geo URI 前缀:

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

              【讨论】:

                【解决方案10】:

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

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

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

                【讨论】:

                  猜你喜欢
                  • 2021-05-21
                  • 1970-01-01
                  • 2013-03-09
                  • 2015-05-22
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-06-19
                  • 1970-01-01
                  相关资源
                  最近更新 更多