android webview删除缓存

[1].[代码] 删除保存于手机上的缓存. 跳至 [1] [2] [3]

 
01// clear the cache before time numDays    
02private 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//优先使用缓存:
2WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 
3 
4//不使用缓存:
5WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
 

[3].[代码] 在退出应用的时候加上如下代码 跳至 [1] [2] [3]

01File 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");

转载于:https://www.cnblogs.com/hudabing/p/3186004.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值