一,缓存介绍
缓存分为:页面缓存和数据缓存
页面缓存: 指加载一个网页时的html、JS、CSS等页面或者资源数据。
数据缓存 : 数据缓存分为AppCache和DOM Storage两种。
一般清除指的是数据缓存;
注意:以下清理缓存的方法,没有区分是那种缓存;
二,各种清楚缓存的方法
1,清除数据库缓存
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
2,清楚历史
webView.clearHistory();
3,清空Cookie
关于这个有几种写法:
a
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
b
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(ValueCallback);
c
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
}
d
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeSessionCookies(null);
cookieManager.removeAllCookie();
cookieManager.flush();
} else {
cookieManager.removeSessionCookies(null);
cookieManager.removeAllCookie();
CookieSyncManager.getInstance().sync();
}
4,清空Localstorage
WebStorage.getInstance().deleteAllData(); //清空WebView的localStorage
5,其他方法:设置统一的缓存路径,然后需要清楚数据时候,遍历每个路径下的文件然后一一删除;
三,其他和缓存相关的API
webView.clearFormData();
//设置缓存模式
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//有五种缓存模式
//设置数据库缓存路径
webView.getSettings().setDatabasePath(cacheDirPath);
//设置应用缓存目录
webView.getSettings().setAppCachePath(cacheDirPath);
//DOM存储功能
webView.getSettings().setDomStorageEnabled(true);
//数据库存储功能
webView.getSettings().setDatabaseEnabled(true);
//应用缓存
webView.getSettings().setAppCacheEnabled(true);
四,其他
调用系统浏览器去下载文件(loadUrl是下载地址):
Intent intent= new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(loadUrl);
intent.setData(content_url);
startActivity(Intent.createChooser(intent, "请选择浏览器"));