使用android webview在浏览器中打开外部链接 [英] Open external links in the browser with android webview

查看:215
本文介绍了使用android webview在浏览器中打开外部链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,但不是因为它有效,它一直在 webview 中打开,我想要的是这些链接不属于我的网站,在您的默认浏览器中打开.任何的想法?谢谢

I have this code, but not because it works, it keeps opening in webview and what I want is that the links do not belong to my website open in your default browser. Any idea? thanks

private class CustomWebViewClient extends WebViewClient {
        @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
              if(url.contains("message2space.es.vu")){
                view.loadUrl(url);
                return true;
            }else{
                return super.shouldOverrideUrlLoading(view, url);
            }

            }
        }

推荐答案

问题是你需要发送一个 意图到默认网络浏览器以打开链接.您正在做的只是在您的 Webview 中调用不同的方法来处理链接.每当您希望另一个应用程序处理某些事情时,您都需要使用 Intent.试试这个代码.

The problem is you need to send an Intent to the default web browser to open the link. What you are doing is just calling a different method in your Webview to handle the link. Whenever you want another app to handle something you need to use Intents. Try this code instead.

private class CustomWebViewClient extends WebViewClient {
        @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
              if(url.contains("message2space.es.vu")) {
                view.loadUrl(url);
              } else {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(i);
              }
              return true;
            }
        }

这篇关于使用android webview在浏览器中打开外部链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆