【问题标题】:How to filter phone and other web links in WebView Android如何在 WebView Android 中过滤手机和其他网页链接
【发布时间】:2015-04-10 19:06:09
【问题描述】:

我想要的是将链接保留在 Web 视图中,只要它们是网站的一部分,但外部链接应该启动到外部 Web 浏览器中。同样在网站上,如果我使用电话号码下方的代码工作并启动电话应用程序但外部链接不起作用,我有电话链接与电话:555-323-2323。

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        if (url.contains("tel:")) {
            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
            return true;
        } else {
            return true;
        } 
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;

【问题讨论】:

    标签: android webview external


    【解决方案1】:

    事情不工作的主要原因是因为你有一个 if-else 并且两个分支都返回 true。 else 语句之后的所有代码都无法访问。删除 else 语句或更改该逻辑。

    【讨论】:

      【解决方案2】:
      • 删除第一条语句 (view.loadUrl(url))。
      • 删除第一个条件的else 部分。

      您的代码将如下所示:

      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (url.contains("tel:")) {
              startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
              return true;
          } 
          if (Uri.parse(url).getHost().equals("www.example.com"))
              // This is my web site, so do not override; let my WebView load the page
              return false;
      
          // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs  
          Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent);
          return true;
      } 
      

      【讨论】:

        【解决方案3】:

        我使用这个解决方案:

         @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("tel:")) {
                    startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
                    return true;
                }
                if (Uri.parse(url).getHost().equals(URL)) {
                    // This is my web site, so do not override; let my WebView load the page
                    return super.shouldOverrideUrlLoading(view, url);
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-12
          • 1970-01-01
          • 1970-01-01
          • 2016-03-25
          • 1970-01-01
          • 2011-03-01
          • 2018-05-13
          • 1970-01-01
          相关资源
          最近更新 更多