WebView链接点击打开默认浏览器

发布于 2024-10-03 19:07:38 字数 163 浏览 4 评论 0原文

现在我有一个加载网络视图的应用程序,并且所有点击都保存在应用程序内。我想做的是当某个链接(例如 http://www.google.com)在应用程序中单击它会打开默认浏览器。如果有人有任何想法,请告诉我!

Right now I have an app that loads a webview and all the clicks are kept within the app. What I would like to do is when a certain link, for example, http://www.google.com is clicked within the app it opens the default browser. If anyone has some ideas please let me know!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

爱冒险 2024-10-10 19:07:38

我今天必须做同样的事情,并且我在 StackOverflow 上找到了一个非常有用的答案,我想在这里分享,以防其他人需要它。

来源(来自sven

webView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            return false;
        }
    }
});

I had to do the same thing today and I have found a very useful answer on StackOverflow that I want to share here in case someone else needs it.

Source (from sven)

webView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            return false;
        }
    }
});
梦途 2024-10-10 19:07:38
WebView webview = (WebView) findViewById(R.id.webview);
webview.loadUrl(https://whatoplay.com/);

您不必包含此代码。

// webview.setWebViewClient(new WebViewClient());

而是使用下面的代码。

webview.setWebViewClient(new WebViewClient()
{
  public boolean shouldOverrideUrlLoading(WebView view, String url)
  {
    String url2="https://whatoplay.com/";
     // all links  with in ur site will be open inside the webview 
     //links that start ur domain example(http://www.example.com/)
    if (url != null && url.startsWith(url2)){
      return false;
    } 
     // all links that points outside the site will be open in a normal android browser
    else
    {
      view.getContext().startActivity(
      new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
      return true;
    }
  }
});
WebView webview = (WebView) findViewById(R.id.webview);
webview.loadUrl(https://whatoplay.com/);

You don't have to include this code.

// webview.setWebViewClient(new WebViewClient());

Instead use below code.

webview.setWebViewClient(new WebViewClient()
{
  public boolean shouldOverrideUrlLoading(WebView view, String url)
  {
    String url2="https://whatoplay.com/";
     // all links  with in ur site will be open inside the webview 
     //links that start ur domain example(http://www.example.com/)
    if (url != null && url.startsWith(url2)){
      return false;
    } 
     // all links that points outside the site will be open in a normal android browser
    else
    {
      view.getContext().startActivity(
      new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
      return true;
    }
  }
});
逆光飞翔i 2024-10-10 19:07:38

您只需要添加以下行

yourWebViewName.setWebViewClient(new WebViewClient());

即可查看this以获取官方文档。

You only need to add the following line

yourWebViewName.setWebViewClient(new WebViewClient());

Check this for official documentation.

维持三分热 2024-10-10 19:07:38

你可以使用 Intent 来实现这一点:

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url"));
startActivity(browserIntent);

you can use Intent for this:

Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("your Url"));
startActivity(browserIntent);
像极了他 2024-10-10 19:07:38

您可以为此使用 Intent:

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

You can use an Intent for this:

Uri uriUrl = Uri.parse("http://www.google.com/"); 
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);  
startActivity(launchBrowser);  
青瓷清茶倾城歌 2024-10-10 19:07:38

由于这是有关 WebView 中外部重定向的首要问题之一,因此这里有一个 Kotlin 上的“现代”解决方案:

webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            val url = request?.url ?: return false
            //you can do checks here e.g. url.host equals to target one
            startActivity(Intent(Intent.ACTION_VIEW, url))
            return true
        }
    }

As this is one of the top questions about external redirect in WebView, here is a "modern" solution on Kotlin:

webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            val url = request?.url ?: return false
            //you can do checks here e.g. url.host equals to target one
            startActivity(Intent(Intent.ACTION_VIEW, url))
            return true
        }
    }
梦旅人picnic 2024-10-10 19:07:38

现在,public boolean shouldOverrideUrlLoading(WebView view, String url) 已被弃用,并使用 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request),您可以在Java:

private class MyWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (Objects.equals(request.getUrl().toString(), "http://www.google.com/")) {
                Uri uri = request.getUrl();
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
                return true;
            }
            else {
                return false;
            }
        }
    }

只需将 http://www.google.com/ 替换为您要在网络中打开的网址浏览器而不是网页视图。

Now that public boolean shouldOverrideUrlLoading(WebView view, String url) is deprecated and public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) is used instead, you can use the following code in Java:

private class MyWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (Objects.equals(request.getUrl().toString(), "http://www.google.com/")) {
                Uri uri = request.getUrl();
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
                return true;
            }
            else {
                return false;
            }
        }
    }

Just replace http://www.google.com/ with the URL you want to open in web browser instead of webview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文