[英]Delete cached files - WebView Android 4.4+
我設法刪除了由WebView創建的緩存文件:
清除android緩存 , 在android中退出時清除應用程序緩存
但是對於Android 4.4,該解決方案無法正常工作,因為文件緩存在:
/data/data/com.app.package/app_webview/
代替:
/data/data/com.app.package/cache/
上面的路徑可以通過官方命令getCacheDir()
。
一種方法可能是對通過Get Application Directory獲得的路徑進行硬編碼
但是,有沒有[官方] /適當的解決方案來解決這個問題?
你可以使用這段代碼
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (String aChildren : children) {
boolean success = deleteDir(new File(dir, aChildren));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir != null && dir.delete();
}
void trimCache() {
try {
String pathadmob = this.getFilesDir().getParent() + "/app_webview";
File dir = new File(pathadmob);
if (dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
此外,這里生成了所有admob緩存4.4+,您可以使用代碼來驗證用戶使用該應用程序的次數,並在用戶達到限制時刪除admob緩存。
通常,要清除WebView緩存,請使用此WebView API: WebView.clearCache(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.