WebView 错误页加载后刷新回原页面
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBlockNetworkImage(false);
settings.setDomStorageEnabled(true);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
//跳转网页仍然在当前WebView中显示
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//保存加载错误界面前的url
if (!url.contains("error/index.html")){
webView.setTag(url);
}
}
// 旧版本,会在新版本中也可能被调用,所以加上一个判断,防止重复显示
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.d("WebViewEmptyActivity", "error.getErrorCode():" +errorCode+description);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
return;
}
// 在这里显示自定义错误页
showErrorHtml(view);
}
// 新版本,只会在Android6及以上调用
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Log.d("WebViewEmptyActivity", "error.getErrorCode():" + error.getErrorCode()+error.getDescription());
if (request.isForMainFrame()){ // 或者: if(request.getUrl().toString() .equals(getUrl()))
showErrorHtml(view);
}
}
});
webView.setWebChromeClient(new WebChromeClient(){
/**
* 当WebView加载之后,返回 HTML 页面的标题 Title
* @param view
* @param title
*/
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
// android 6.0 以下通过title获取判断
if (!TextUtils.isEmpty(title)) {
if (title.contains("404") || title.contains("500") || title.contains("Error") || title.contains("找不到网页") || title.contains("网页无法打开")) {
showErrorHtml(view);
}
}
}
});
webView.loadUrl("https://www.baidu.com");
private void showErrorHtml(WebView view) {
view.loadUrl("about:blank");// 避免出现默认的错误界面
webView.loadUrl("file:///android_asset/error/index.html")
}
public void refresh(){
webView.clearCache(true);
Object tag = webView.getTag();
if (tag!=null){
webView.loadUrl(tag.toString());
return;
}
webView.reload();
}
WebView 与H5 交互
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new AndroidForJs(this),"AndroidView");
public class AndroidForJs {
private Context context;
public AndroidForJs(Context context) {
this.context = context;
}
//api17以后,只有public且添加了 @JavascriptInterface 注解的方法才能被调用
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(context, "js调用android showToast():" + toast, Toast.LENGTH_SHORT).show();
}
}
H5
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script type="text/javascript">
function myFunction(toast){
alert("stoast"+toast);
//AndroidView与原生约定的字段
window.AndroidView.showToast(toast);
}
</script>
</head>
<body>
<p>单击按钮触发函数。</p>
<button onclick="myFunction('aaa')">点我</button>
<p id="demo"></p>
</body>
</html>
网友评论