// 硬件加速
getActivity().getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
// WebView 配置
WebSettings webSettings = mWebView.getSettings();
// 生命周期
mWebView.onPause(); // 通过 onPause 动作通知内核暂停所有的动作,如 DOM 的解析、plugin 的执行、JavaScript 执行等
mWebView.onResume(); // 恢复 WebView,能正常执行网页的响应
((ViewGroup) mWebView.getParent()).removeView(mWebView);
mWebView.destroy(); // 当 Activity 要 destroy 时,应先将 WebView 移除,再 destroy 掉
// 前进后退
if (mWebView.canGoBack()) {
mWebView.goBack();
}
if (mWebView.canGoForward()) {
mWebView.goForward();
}
// 缓存相关
mWebView.clearCache(true); // 清除缓存
mWebView.clearHistory(); // 清除历史
mWebView.clearFormData(); // 清除表单数据
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);// 设置缓存模式
// 缓存模式
LOAD_DEFAULT: 默认,根据 cache-control 决定是否从网络上取数据
LOAD_NORMAL: API level 17 中已经废弃, 从API level 11开始作用同 LOAD_DEFAULT 模式
LOAD_CACHE_ELSE_NETWORK: 只要本地有,无论是否过期,或者 no-cache,都使用缓存中的数据
LOAD_NO_CACHE: 不使用缓存,只从网络获取数据
LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据
// js 相关
webSettings.setJavaScriptEnabled(true); // 支持 js。如果碰到后台无法释放 js 导致耗电,应在 onStop 和 onResume 里分别设成 false 和 true
mWebView.addJavascriptInterface(new WebAppInterface(this), "android"); // js 接口
webSettings.setPluginsEnabled(true); // 支持插件
// 设置自适应屏幕,两者合用
webSettings.setUseWideViewPort(true); // 将图片调整到适合 WebView 的大小
webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
// 缩放操作
webSettings.setSupportZoom(true); // 支持缩放,默认为 true
webSettings.setBuiltInZoomControls(true); // 设置内置的缩放控件,若为 false,则该 WebView 不可缩放
webSettings.setDisplayZoomControls(false); // 隐藏原生的缩放控件
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.setWebChromeClient(new MyWebChromeClient());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
mWebView.removeJavascriptInterface("searchBoxJavaBridge_");
mWebView.removeJavascriptInterface("accessibility");
mWebView.removeJavascriptInterface("accessibilityTraversal");
}
webSettings.setSavePassword(false);
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
showCustErrorPage();
}
private View mErrorView;
protected void showCustErrorPage() {
// 移除WebView
ViewGroup webParentView = (ViewGroup) mWebViewInstance.getParent();
while (webParentView.getChildCount() > 0) {
webParentView.removeViewAt(0);
}
// 生成自定义错误页面
if (mErrorView == null) {
mErrorView = View.inflate(mContext, R.layout.webview_error, null);
mErrorView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showWebViewPage();
mWebViewInstance.reload();
}
});
}
// 替换为错误页面
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewPager.LayoutParams.FILL_PARENT, ViewPager.LayoutParams.FILL_PARENT);
webParentView.addView(mErrorView, 0, lp);
}
protected void showWebViewPage() {
// 移除自定义错误页面
ViewGroup webParentView = (ViewGroup) mErrorView.getParent();
while (webParentView.getChildCount() > 0) {
webParentView.removeViewAt(0);
}
// 替换为WebView
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewPager.LayoutParams.FILL_PARENT, ViewPager.LayoutParams.FILL_PARENT);
webParentView.addView(mWebViewInstance, 0, lp);
}
public void releaseAllWebViewCallback() {
if (android.os.Build.VERSION.SDK_INT < 16) {
try {
Field field = WebView.class.getDeclaredField("mWebViewCore");
field = field.getType().getDeclaredField("mBrowserFrame");
field = field.getType().getDeclaredField("sConfigCallback");
field.setAccessible(true);
field.set(null, null);
} catch (NoSuchFieldException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
}
} else {
try {
Field sConfigCallback = Class.forName("android.webkit.BrowserFrame").getDeclaredField("sConfigCallback");
if (sConfigCallback != null) {
sConfigCallback.setAccessible(true);
sConfigCallback.set(null, null);
}
} catch (NoSuchFieldException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
}
}
}
public void updateCookies(String url, String value) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) { // 2.3及以下
CookieSyncManager.createInstance(getContext().getApplicationContext());
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, value);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
CookieSyncManager.getInstance().sync();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有