关于webview的缓存,在不同设备上会有不同的目录。
APP的登录状态和H5的登录状态常常因为某些原因不能同步。
故想要每次关闭webview页面时,都可以清除h5缓存的数据。
通过分析,web页面常用的存储有:cookie、localStorage、session
最终实现用一下方法清除缓存(有时会失效,极小概率)
WebStorage.getInstance().deleteAllData();
当然,也尝试过其他方法
CookieSyncManager.createInstance(getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeSessionCookies(null);
cookieManager.removeAllCookie();
cookieManager.flush();
} else {
cookieManager.removeAllCookie();
CookieSyncManager.getInstance().sync();
}
deleteFile(new File(getCacheDir().getParentFile().getAbsolutePath(),"app_webview"));
public void deleteFile(File file) {
if (file.exists()) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
File files[] = file.listFiles();
for (File value : files) {
deleteFile(value);
}
}
// Timber.d("deleteFile delete file path=" + file.getAbsolutePath()+","+file.delete());
new SecurityManager().checkDelete(file.getAbsolutePath());
Timber.d("deleteFile delete file path=" + file.getAbsolutePath());
} else {
Timber.d("deleteFile delete file no exists " + file.getAbsolutePath());
}
}