I'm trying to clear cache of all the applications installed on my phone.
Here is my code:
public void clearApplicationData() {
File cache = getCacheDir();
File parent1 = new File(cache.getParent());
File parent2 = new File(parent1.getParent());
if(parent2.exists()) {
String[] children = parent2.list();
for(String s : children){
if(!s.equals("lib")){
deleteDir(new File(parent2, s));
Log.d("TAG", "File /data/data/APP_PACKAGE/" + s +" DELETED");
}
}
}
}
public static boolean deleteDir(File dir) {
Log.d("TAG", "Deleting :" + dir.getPath());
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]));
Log.d("TAG", "Delete :" + success +" \n");
if (!success) {
return false;
}
}
}
return dir.delete();
}
In manifest file I declared this permission :
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
But always I'm getting parent2
as null
.
I can able to delete only my application cache data using parent1
.
Any suggestions please.