如何使通过链接根据域名默认浏览器中的WebView打开或打开? [英] How to make links open within webview or open by default browser depending on domain name?

查看:169
本文介绍了如何使通过链接根据域名默认浏览器中的WebView打开或打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WebView中,我要打开的链接都属于域www.example.org在web视图,而所有其他环节(如点击)我的应用程序之外的默认浏览器中打开。

I have WebView in which I want to open links belong to domain www.example.org in webview while all other links (if clicked) open by the default browser outside of my application.

我试图用公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL),但它不能正常工作。

I tried to use public boolean shouldOverrideUrlLoading(WebView view, String url) but it does not work properly.

下面是code不工作:

Here is the code that does not work:

public class MyWebViewClient extends WebViewClient {
    @Override
               public boolean shouldOverrideUrlLoading(WebView view, String url) {
                   try {
                   URL urlObj = new URL(url);
                   if (urlObj.getHost().equals("192.168.1.34")) {
                       view.loadUrl(url);
                       return true;
                   } else {
                       view.loadUrl(url);
                       return false;
                     }
                   } catch (Exception e) {

                   }
               }
}

在这两种情况下(返回true,并返回false)的网址是我的应用程序处理。

In both cases ( return true and return false) the URL is handled by my application.

推荐答案

一旦创建并附加 WebViewClient 的WebView ,你已经覆盖了默认的行为,其中Android将允许ActivityManager到URL传递给浏览器(这只有在没有客户端上设置视图时),请参阅<一href="http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29">the文档的方法更。

Once you create and attach a WebViewClient to your WebView, you have overridden the default behavior where Android will allow the ActivityManager to pass the URL to the browser (this only occurs when no client is set on the view), see the docs on the method for more.

在连接了 WebViewClient ,返回假表 shouldOverrideUrlLoading()传递的URL 的WebView ,而返回true告诉的WebView 来什么也不做......因为你的应用程序将照顾它。不幸的是,这些路径会导致让机器人将URL传递到浏览器。这样的事情应该解决您的问题:

Once you have attached a WebViewClient, returning false form shouldOverrideUrlLoading() passes the url to the WebView, while returning true tells the WebView to do nothing...because your application will take care of it. Unfortunately, neither of those paths leads to letting Android pass the URL to the browser. Something like this should solve your issue:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    try {
      URL urlObj = new URL(url);
      if( TextUtils.equals(urlObj.getHost(),"192.168.1.34") ) {
        //Allow the WebView in your application to do its thing
        return false;
      } else {
        //Pass it to the system, doesn't match your domain
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
        //Tell the WebView you took care of it.
        return true;
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
}

我知道这似乎有点违反直觉,你会期望返回false; 来完全规避的WebView ,但这不,一旦你使用自定义的情况下 WebViewClient

I know that seems a little counterintuitive as you would expect return false; to completely circumvent the WebView, but this is not the case once you are using a custom WebViewClient.

希望帮助!

这篇关于如何使通过链接根据域名默认浏览器中的WebView打开或打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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