我希望能够在Android Marshmallow 6.0中以编程方式清除我的应用程序缓存。 我尝试了以下代码,但它在Marshmallow中不起作用。我在stackoverflow上读到下面的代码从API级别19开始被弃用。我在我的Manifests.xml中添加了CLEAR_APP_CACHE权限。
public void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public 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;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}