首页 > Android > 正文

Android WebView删除缓存

2016-09-19 Android 527 ℃ 0 评论

1. 删除保存于手机上的缓存

// clear the cache before time numDays     
private int clearCacheFolder(File dir, long numDays) {          
    int deletedFiles = 0;         
    if (dir!= null && dir.isDirectory()) {             
        try {                
            for (File child:dir.listFiles()) {    
                if (child.isDirectory()) {              
                    deletedFiles += clearCacheFolder(child, numDays);          
                }    
                if (child.lastModified() < numDays) {     
                    if (child.delete()) {                   
                        deletedFiles++;           
                    }    
                }    
            }             
        } catch(Exception e) {       
            e.printStackTrace();    
        }     
    }       
    return deletedFiles;     
}


activity中的代码:

webView.clearCache(true);
clearCacheFolder(getApplicationContext().getCacheDir(),
System.currentTimeMillis());
getApplicationContext().deleteDatabase("webview.db");
getApplicationContext().deleteDatabase("webviewCache.db");
clearCacheFolder(getApplicationContext().getCacheDir(),
System.currentTimeMillis());
getApplicationContext().deleteDatabase("we