【问题标题】:Android : Open a URL in a Browser [duplicate]Android:在浏览器中打开一个 URL [重复]
【发布时间】:2011-09-10 18:18:34
【问题描述】:

可能重复:
How can I open a URL in Android’s web browser from my application?

我一直在尝试找出如何创建在指定浏览器中打开指定 URL 的意图。浏览器可能并不总是默认的。 但是我做不到。

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个:

    String url = "your URL";
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(browserIntent);
    

    【讨论】:

    • -1 使用 Intent.ACTION_VIEW 常量
    • 如果有人试图提供帮助,人们投票 -1,:),这是一件好事 ^^,仅供参考:上面的代码对我来说很好,
    • 帮助总是很感激 :) 常量也总是比字符串好。
    • 工作代码并不意味着应该鼓励它(-1 只是指向我觉得不正确的东西)。总是更喜欢现有的常量而不是硬编码的值。 (常量不应在您编辑的答案中的双引号内)。
    • 您可以简单地对其进行编辑以修复...。引号问题:)
    【解决方案2】:

    使用 Intent.ACTION_VIEW 常量作为 Intent 操作,使用 url 作为数据。

    final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    activity.startActivity(intent);
    

    请注意,该 URL 必须是完整的 URL(以 http:// 或 https:// 开头),因此请检查您的代码,确保该 URL 不是诸如 www.google.com 之类的短格式(如果它是用户)-已定义。

    【讨论】:

      【解决方案3】:

      你可以使用其中任何一个,也可以阅读Link

       Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com")); 
      startActivity(browserIntent); 
      

      或

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

      【讨论】:

      • 当适当的常量存在于某处时,不要鼓励使用硬编码值。该代码将来可能会中断。
      • @MarvinLabs:好的,Thnx。我会记住的。非常有用的建议。
      猜你喜欢
      • 2015-05-27
      • 2012-01-22
      • 2015-06-19
      • 1970-01-01
      • 2015-05-22
      • 2012-07-13
      • 1970-01-01
      • 2015-10-22
      相关资源
      最近更新 更多