【问题标题】:webview to open sites of certain domainwebview 打开特定域的站点
【发布时间】:2019-07-11 19:15:32
【问题描述】:

我正在尝试制作一个应用程序,它可以在 webview 中打开某个站点的所有网页,例如 www.yahoo.com,但在默认浏览器中打开所有其他网页。这是我正在使用但无法完全正常工作的代码。

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("http://www.yahoo.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;
    }
}

在我看来,这段代码应该可以工作,但是当我加载 yahoo 时,它仍然会转到外部浏览器。任何帮助,将不胜感激。谢谢!

【问题讨论】:

    标签: android webview


    【解决方案1】:

    这是一种不同的方法。

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String urlHost = Uri.parse(url).getHost();
        switch (urlHost) {
            case "yahoo.com":
                return false;
            case "www.yahoo.com":
                return false;
            case "m.yahoo.com":
                return false;
            default:
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
        }
    }
    

    【讨论】:

      【解决方案2】:

      getHost() 通常只返回域名,或者有时会以www. 为前缀。它从不包含http:// 协议 AFAIK。尝试使用:

      if ((Uri.parse(url).getHost().equals("yahoo.com")) ||  (Uri.parse(url).getHost().equals("www.yahoo.com")) ||  (Uri.parse(url).getHost().equals("m.yahoo.com"))) {
          //Your code
      

      【讨论】:

      • 不幸的是,该代码不能解决问题。当 yahoo.com 在浏览器而不是 webview 中打开时,url 是 m.yahoo.com。也许这与它有关?
      【解决方案3】:

      试试这个应该对你有帮助

      private WebView mWebview;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_yahoo);
      
              mWebview  = new WebView(this);
      
              mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
      
              final Activity activity = this;
      
              mWebview.setWebViewClient(new WebViewClient() {
                  @SuppressWarnings("deprecation")
                  @Override
                  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                      Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
                  }
                  @TargetApi(android.os.Build.VERSION_CODES.M)
                  @Override
                  public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
                      // Redirect to deprecated method, so you can use it in all SDK versions
                      onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
                  }
              });
      
              mWebview .loadUrl("http://www.yahoo.com");
              setContentView(mWebview );
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多