Skip to content

Commit

Permalink
add 增加 onPageStarted() 和 onReceivedSslError()
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Aug 10, 2020
1 parent 7677d27 commit 2557712
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
44 changes: 28 additions & 16 deletions ByWebView/src/main/java/me/jingbin/web/ByWebViewClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions ByWebView/src/main/java/me/jingbin/web/OnByWebClientCallback.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package me.jingbin.web;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;

/**
* Created by jingbin on 2020/6/30.
*/
public abstract class OnByWebClientCallback {

public void onPageStarted(WebView view, String url, Bitmap favicon) {

}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

/**
* 网页可以处理:
Expand Down Expand Up @@ -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) {
// 网页加载完成后的回调
Expand Down

0 comments on commit 2557712

Please sign in to comment.