我想在android中启动我的应用程序时为清除缓存内存编程,任何人都知道编码请给我。
全部谢谢
答案 0 :(得分:2)
试试这个
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
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;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
并在manifest.xml中添加此行
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"></uses-permission>