android webview删除缓存
[1].[代码] 删除保存于手机上的缓存. 跳至 [1] [2] [3]
01 |
// clear the cache before time numDays |
02 |
private int clearCacheFolder(File dir, long numDays) { |
03 |
int deletedFiles = 0 ; |
04 |
if (dir!= null && dir.isDirectory()) { |
05 |
try { |
06 |
for (File child:dir.listFiles()) { |
07 |
if (child.isDirectory()) { |
08 |
deletedFiles += clearCacheFolder(child, numDays); |
09 |
} |
10 |
if (child.lastModified() < numDays) { |
11 |
if (child.delete()) { |
12 |
deletedFiles++; |
13 |
} |
14 |
} |
15 |
} |
16 |
} catch (Exception e) { |
17 |
e.printStackTrace(); |
18 |
} |
19 |
} |
20 |
return deletedFiles; |
21 |
} |
[2].[代码] 打开关闭使用缓存 跳至 [1] [2] [3]
1 |
//优先使用缓存: |
2 |
WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); |
3 |
4 |
//不使用缓存: |
5 |
WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); |
[3].[代码] 在退出应用的时候加上如下代码 跳至 [1] [2] [3]
01 |
File file = CacheManager.getCacheFileBaseDir(); |
02 |
if (file != null && file.exists() && file.isDirectory()) { |
03 |
for (File item : file.listFiles()) { |
04 |
item.delete(); |
05 |
} |
06 |
file.delete(); |
07 |
} |
08 |
|
09 |
context.deleteDatabase( "webview.db" ); |
10 |
context.deleteDatabase( "webviewCache.db" ); |
路漫漫其修远兮 吾将上下而求索