Webview用法
WebSettings webSettings = mWebView.getSettings();
//设置支持本地文件获取和缓存
webSettings.setAllowFileAccess(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccessFromFileURLs(true);
}
webSettings.setJavaScriptEnabled(true);
//解除网络图片数据阻止
webSettings.setBlockNetworkImage(false);
//设置h5缓存
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCacheMaxSize(1024 * 1024 * 8);
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
webSettings.setAppCachePath(appCachePath);
mWebView.setHorizontalScrollBarEnabled(false);//水平不显示
mWebView.setVerticalScrollBarEnabled(false); //垂直不显示
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//错误提示
showToast("当前网络不可用,请检查网络");
//错误处理
try {
mWebView.stopLoading();
mWebView.clearView();
} catch (Exception e) {
}
if (mWebView.canGoBack()) {
mWebView.goBack();
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
try {
mLayout.refreshFinish(PullToRefreshLayout.SUCCEED);//页面加载完成,可自处理刷新完成
pageFinish = true;
} catch (Exception e) {
e.printStackTrace();
Log.e("hjh", e.getMessage());
}
super.onPageFinished(view, url);
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
});
mWebView.setOverScrollMode(View.OVER_SCROLL_NEVER);
mWebView.addJavascriptInterface(myJSInterface, "Android");与JS的交互,myJSInterface为交互类实例,后面参数相当于别名,js端使用;交互类中与js交互的方法需要@JavascriptInterface注解表明
mWebView.loadUrl("javascript:app.eventHandler('refresh')");调用JS端的方法,无参;
mWebView.loadUrl("javascript:app.eventHandler('refresh','参数')");一个参数;
mWebView.loadUrl("javascript:app.eventHandler('delete',["+id+"])");参数为数组
String url = getResources().getStringArray(R.array.FindUrlArray)[1]+findString("url");id = findString("id");mWebView.loadUrl(url);
//对webview返回监听,可对内部跳转后的返回
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {//
if(getWebView().canGoBack()){
getWebView().goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
本次是各activity嵌套Webview与Js交互界面跳转,出现了非栈顶图片解析异常,经过排查为首页调用了
mWebView.pauseTimers();mWebView.resumeTimers();
//造成栈顶webview图片加载异常
因此此方法在上述情况下,需谨慎使用,会造成后面webview加载事件的中断
webview与控件同时存在于界面的异常:webview上面有图片或者文字,当内容过多会在控件上造成阴影,处理方法:采用webview加载完成后延迟半秒在显示底部的控件;
private void removeAllCookie()
{
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(wb.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
//String testcookie1 = cookieManager.getCookie(urlpath);
cookieManager.removeAllCookie();
cookieSyncManager.sync();
//String testcookie2 = cookieManager.getCookie(urlpath);
}