首页 文章

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

提问于
浏览
656

这个问题在这里已有答案:

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

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

10 回答

  • 6

    来自XML

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

    android:autoLink="web"
    

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

  • 140

    短版

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

    也应该工作......

  • 9

    “还有一种方法可以将coords直接传递给谷歌 Map 显示吗?”

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

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

  • 11

    最短的版本 .

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

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

    在代码中使用以下代码段

    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

  • 1556

    还有一种方法可以将coords直接传递给google Map 进行显示吗?

    您可以使用geo URI 前缀:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("geo:" + latitude + "," + longitude));
    startActivity(intent);
    
  • 171
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    startActivity(browserIntent);
    
  • 79

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

  • 25

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

    String url = "http://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

    =)

相关问题