【发布时间】:2014-05-28 09:47:42
【问题描述】:
以编程方式清除 android 应用程序中的缓存的正确方法是什么。我已经在使用以下代码,但它看起来不适合我
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
clearApplicationData();
}
public void clearApplicationData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));
Log.i("EEEEEERRRRRRROOOOOOORRRR", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
【问题讨论】:
-
还要检查 getExternalCacheDir()
-
@for3st 我们可以只为 webview 清除缓存吗?
-
WebView.clearCache(boolean includeDiskFiles) 将清除应用程序中所有 webvie 的缓存
-
@RizwanAhmed 你找到解决上述问题的方法了吗
标签: android