From 2557712340a80e66f107c40f2e0dd5bd4d93c56a Mon Sep 17 00:00:00 2001 From: jingbin <770413277@qq.com> Date: Mon, 10 Aug 2020 21:27:05 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=A2=9E=E5=8A=A0=20onPageStarted()=20?= =?UTF-8?q?=E5=92=8C=20onReceivedSslError()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/jingbin/web/ByWebViewClient.java | 44 ++++++++++++------- .../me/jingbin/web/OnByWebClientCallback.java | 14 ++++++ .../webviewstudy/ui/ByWebViewActivity.java | 17 ++++++- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java b/ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java index df622f9..f0feb49 100644 --- a/ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java +++ b/ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java @@ -2,6 +2,7 @@ import android.app.Activity; import android.content.DialogInterface; +import android.graphics.Bitmap; import android.net.http.SslError; import android.os.Build; import android.support.annotation.RequiresApi; @@ -75,6 +76,13 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) { } } + @Override + public void onPageStarted(WebView view, String url, Bitmap favicon) { + if (onByWebClientCallback != null) { + onByWebClientCallback.onPageStarted(view, url, favicon); + } + super.onPageStarted(view, url, favicon); + } @Override public void onPageFinished(WebView view, String url) { @@ -127,22 +135,26 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc */ @Override public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) { - AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); - builder.setMessage("SSL认证失败,是否继续访问?"); - builder.setPositiveButton("继续", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - handler.proceed(); - } - }); - builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - handler.cancel(); - } - }); - AlertDialog dialog = builder.create(); - dialog.show(); + if (onByWebClientCallback == null || !onByWebClientCallback.onReceivedSslError(view, handler, error)) { + AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); + builder.setMessage("SSL认证失败,是否继续访问?"); + builder.setPositiveButton("继续", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + handler.proceed(); + } + }); + builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + handler.cancel(); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } else { + onByWebClientCallback.onReceivedSslError(view, handler, error); + } } /** diff --git a/ByWebView/src/main/java/me/jingbin/web/OnByWebClientCallback.java b/ByWebView/src/main/java/me/jingbin/web/OnByWebClientCallback.java index d99d24b..5e9dab8 100644 --- a/ByWebView/src/main/java/me/jingbin/web/OnByWebClientCallback.java +++ b/ByWebView/src/main/java/me/jingbin/web/OnByWebClientCallback.java @@ -1,5 +1,8 @@ package me.jingbin.web; +import android.graphics.Bitmap; +import android.net.http.SslError; +import android.webkit.SslErrorHandler; import android.webkit.WebView; /** @@ -7,6 +10,10 @@ */ public abstract class OnByWebClientCallback { + public void onPageStarted(WebView view, String url, Bitmap favicon) { + + } + public void onPageFinished(WebView view, String url) { } @@ -14,4 +21,11 @@ public void onPageFinished(WebView view, String url) { public boolean isOpenThirdApp(String url) { return !url.startsWith("http:") && !url.startsWith("https:"); } + + /** + * @return true 表示是自己处理的 + */ + public boolean onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { + return false; + } } diff --git a/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java b/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java index bab9512..853a4aa 100644 --- a/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java +++ b/app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java @@ -2,7 +2,9 @@ import android.content.Context; import android.content.Intent; +import android.graphics.Bitmap; import android.net.Uri; +import android.net.http.SslError; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; @@ -12,6 +14,7 @@ import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; +import android.webkit.SslErrorHandler; import android.webkit.WebView; import android.widget.LinearLayout; import android.widget.TextView; @@ -25,8 +28,8 @@ import me.jingbin.web.ByWebTools; import me.jingbin.web.ByWebView; -import me.jingbin.web.OnTitleProgressCallback; import me.jingbin.web.OnByWebClientCallback; +import me.jingbin.web.OnTitleProgressCallback; /** * 网页可以处理: @@ -114,6 +117,18 @@ public void onReceivedTitle(String title) { }; private OnByWebClientCallback onByWebClientCallback = new OnByWebClientCallback() { + + @Override + public void onPageStarted(WebView view, String url, Bitmap favicon) { + Log.e("---onPageStarted", url); + } + + @Override + public boolean onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { + // 如果自己处理,需要返回true + return super.onReceivedSslError(view, handler, error); + } + @Override public void onPageFinished(WebView view, String url) { // 网页加载完成后的回调