美文网首页
WebView之WebViewCilent

WebView之WebViewCilent

作者: 钦_79f7 | 来源:发表于2019-12-18 11:23 被阅读0次

WebViewClient

boolean shouldOverrideUrlLoading

    /**
    * Give the host application a chance to take over the control when a new
    * url is about to be loaded in the current WebView. If WebViewClient is not
    * provided, by default WebView will ask Activity Manager to choose the
    * proper handler for the url. If WebViewClient is provided, return true
    * means the host application handles the url, while return false means the
    * current WebView handles the url.
    * This method is not called for requests using the POST "method".
    *
    * @param view The WebView that is initiating the callback.
    * @param url The url to be loaded.
    * @return True if the host application wants to leave the current WebView
    *         and handle the url itself, otherwise return false.
    * @deprecated Use {@link #shouldOverrideUrlLoading(WebView, WebResourceRequest)
    *             shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead.
    */

根据上述shouldOverrideUrlLoading的官方文档描述,可以看到关于此方法的作用处理存在了三种情况:

  1. 未设置WebViewClient,则WebView会请求ActivityManager选择适合处理URL的APP去处理,一般情况就是启动浏览器去加载URL。
  2. 如果设置了WebViewClient且shouldOverrideUrlLoading返回true,则宿主应用(也就是自己开发的APP)来处理URL。
  3. 如果设置了WebViewClient且shouldOverrideUrlLoading返回false,则是当前WebView加载处理WebView
  • Deprecated
@Deprecated
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return false;
}
  • normal
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    return shouldOverrideUrlLoading(view,request.getUrl().toString());
}

此方法的回调是一直可用的,由于关于WebResourceRequest的相关方法CallRequire API 21,故一般在做处理时,此方法几乎不用

onReceivedError

页面加载错误时的回调。API中存在两个方法,其中一个是deprecated。

==注意==:
在实际使用中发现,存在着页面加载正常,deprecated的方法未被回调,但是此新版本的回调被执行了,导致不应该展示错误页面的时候,却展示错误UI.

故:在实际做业务处理的时候,一般仍然都是在deprecated的方法中做一些逻辑处理。

void onPageFinished

WebView加载完成的回调。

当页面加载错误,onReceivedError()被回调后,仍然会回调onPageFinish()

==注意==:这里的加载完成,仅仅指的是当前页面的mainframe(主框架)加载完成,不代表页面包含的图片等已加载渲染完成。

void onReceivedSslError

WebView加载Https链接时出现Ssl错误时,回调此方法。

默认处理方式是cancel,直接取消当次请求

public void onReceivedSslError(WebView view, SslErrorHandler handler,
        SslError error) {
    handler.cancel();
}

要使得请求继续下去,需要做

handler.proceed();

==注意:==

Google官方并不推荐在onReceivedSslError中直接做handler.proceed()处理。

对于直接proceed处理的APP,Google Play 商店不予通过。

在这里一般是采用一个AlertDialog来提示用户进行选择,而非直接proceed()

@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.notification_error_ssl_cert_invalid);
    builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.proceed();
        }
    });
    builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.cancel();
        }
    });
    final AlertDialog dialog = builder.create();
    dialog.show();
}

WebResourceResponse shouldInterceptRequest

拦截当前请求,并根据当前请求做自定义的返回处理。

即:当我们不需要某种URL默认下的返回结果时,或者不希望某个请求走WebView默认的处理逻辑时,可以在此处拦截住当前请求的执行,并执行自己想要的逻辑,然后将自己想要的结果,返回交给WebView。

  • Deprecated
@Deprecated
public WebResourceResponse shouldInterceptRequest(WebView view,
        String url) {
    return null;
}
  • normal (request.getUrl() Call Require API 21)
public WebResourceResponse shouldInterceptRequest(WebView view,
        WebResourceRequest request) {
    return shouldInterceptRequest(view, request.getUrl().toString());
}

相关文章

网友评论

      本文标题:WebView之WebViewCilent

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