public static void clearApplicationData(Context context) {
File cache = context.getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String list : children) {
Log.d("delete cache ", list);
if(list.equals("shared_prefs")) continue;
deleteDir(new File(appDir, list));
}
}
}
private 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();
}
There's a chord like this.
First of all, you should pay attention to the log and conditional statements of the clearApplicationData function.
If you look at the log
delete cache: cache
delete cache: shared_prefs
delete cache: app_webview
delete cache: code_cache
delete cache: files
delete cache: no_backup
delete cache: databases
delete cache: app_textures
Then if you have to remove the parts that you shouldn't delete
if(list.equals("shared_prefs")) continue;
You can write it like this in the conditional statement.
I wrote the conditions like this because I wanted to leave a shared preference value and delete it.
If there are more than two, not one, please write down elseif at the bottom, and the part will be skipped from the repetition to the continuation, it cannot be deleted.
When you use it, write it down like this.
clearApplicationData(getApplicationContext());