如何在Android应用程序中使用外部浏览器打开链接?

6

有没有人能帮我编写代码,使链接在外部浏览器或其他Android应用程序中打开?

现在的情况是链接在应用程序内部打开。但如果链接属于一个Android应用程序,则无法打开。它会显示安装该Android应用程序。

所以我希望如果链接可以在浏览器中打开,那么它将从浏览器列表中询问。或者如果链接属于应用程序,则必须在列表中显示该应用程序。

4个回答

8
像这样的东西可能会起作用。
Intent browserIntent = new Intent(Intent.ACTION_VIEW,   
Uri.parse("http://www.google.com"));
startActivity(browserIntent);

4
如@zain之前发布的,您可以使用。
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
startActivity(intent); 

但是如果你在设备中安装了多个浏览器,并希望从其中选择一个来使用。可以像这样使用intent选择器:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));

// Always use string resources for UI text. This says something like "Share this photo with"
String title = getResources().getText(R.string.chooser_title);
// Create and start the chooser
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);

从这里参考 在Android中打开链接时显示浏览器列表


这很棒,尽管是一个旧答案,但仍然可以正常工作。谢谢。 另外需要注意的是,如果在Fragment之外,您需要从上下文开始活动。 context.startActivity(chooser) - Forrest

1

enter image description here

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.in/"));


            String title = "Complete Action Using";

            Intent chooser = Intent.createChooser(intent, title);
            startActivity(chooser);

-1
如果您在应用程序的 WebView 中,并且在单击链接时,如果应用程序在应用程序本身中打开链接,则可能应该覆盖此方法。
 myWebView.setWebViewClient(new WebViewClient()       
    {
         @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            //view.loadUrl(url);

            return false;
        }
    });

返回false应该询问用户在哪里打开链接,使用安装在移动设备上的浏览器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接