美文网首页
WebViewClient shouldOverrideUrlL

WebViewClient shouldOverrideUrlL

作者: 孔鹏飞 | 来源:发表于2021-07-02 15:33 被阅读0次

    方法原型

    • 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.
         *
         * <p class="note"><b>Note:</b> 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)}.
         *
         * <p class="note"><b>Note:</b> This method is not called for POST requests.
         *
         * <p class="note"><b>Note:</b> 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;
              }
         });
    

    相关文章

      网友评论

          本文标题:WebViewClient shouldOverrideUrlL

          本文链接:https://www.haomeiwen.com/subject/obveultx.html