【发布时间】:2015-02-24 18:59:34
【问题描述】:
我想清除我的 android 应用程序中的缓存,为了实现这一点,我使用了下面给出的两种方法,但是在调用 deleteCache(getApplicationContext()) 后缓存没有被清除。这个问题被多次问到,但似乎没有一个答案对我有帮助。以下是我使用的方法。感谢您的帮助。
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
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();
}
【问题讨论】:
-
是logcat中显示的错误吗?还是返回 false?
-
感谢 Randyka,logcat 中没有显示错误,它返回 true。