WebViewClient shouldOverrideUrlLoading方法说明

时间:2021-7-21 作者:qvyue

方法原型

  • shouldOverrideUrlLoading(WebView view, String url)

此方法已经被标记为废弃,方法的第二个参数类型为String,在Android 7.0以下系统被调用

    /**
      * @param view The WebView that is initiating the callback.
      * @param url The URL to be loaded.
      * @return {@code true} to cancel the current load, otherwise return {@code false}.
     * @deprecated Use {@link #shouldOverrideUrlLoading(WebView, WebResourceRequest)
     *   shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead.
    */
   @Deprecated
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
   }
  • shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
    此方法的第二个参数类型为WebResourceRequest ,在Android 7.0及以上系统被调用,此方法又调用了上面的方法
    /**
     * @param view The WebView that is initiating the callback.
     * @param request Object containing the details of the request.
     * @return {@code true} to cancel the current load, otherwise return {@code false}.
     */
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return shouldOverrideUrlLoading(view, request.getUrl().toString());
    }

因此在Android 7.0以下系统,如果我们设置了WebViewClient,只有shouldOverrideUrlLoading(WebView view, String url)方法会被调用,在Android 7.0及以上系统shouldOverrideUrlLoading(WebView view, String url)shouldOverrideUrlLoading(WebView view, WebResourceRequest request)两个方法都会被调用。

方法说明

 /**
     * Give the host application a chance to take control when a URL is about to be loaded in the
     * current WebView. If a WebViewClient is not provided, by default WebView will ask Activity
     * Manager to choose the proper handler for the URL. If a WebViewClient is provided, returning
     * {@code true} causes the current WebView to abort loading the URL, while returning
     * {@code false} causes the WebView to continue loading the URL as usual.
     *
     * 

Note: Do not call {@link WebView#loadUrl(String)} with the same * URL and then return {@code true}. This unnecessarily cancels the current load and starts a * new load with the same URL. The correct way to continue loading a given URL is to simply * return {@code false}, without calling {@link WebView#loadUrl(String)}. * *

Note: This method is not called for POST requests. * *

Note: This method may be called for subframes and with non-HTTP(S) * schemes; calling {@link WebView#loadUrl(String)} with such a URL will fail. */

以上语句引自于android.webkit.WebViewClient类,翻译总结一下就是:

  • 该方法给应用程序提供时机,让其控制当前的WebView是否继续加载相应的URL
  • 如果应用程序没有提供WebViewClient,默认情况下WebView会询问系统选择合适的程序处理URL。如果提供了WebViewClient,返回值为true导致当前WebView中止加载URL,返回false导致WebView继续加载当前URL
  • 继续加载给定URL的正确方法是简单地返回false,不需要调用WebView.loadUrl(String),然后返回true

因此阻止WebView调用系统浏览器只需要设置WebViewClient,然后返回false即可。

    webView.setWebViewClient(new WebViewClient(){
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
               return false;
          }
     });
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。