1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Audumbar
  • 404
  • 5
  • 16
  • 1
    http://stackoverflow.com/questions/17313721/how-to-delete-other-applications-cache-from-our-android-app , http://stackoverflow.com/questions/14507092/android-clear-cache-of-all-apps – Shayan Pourvatan Dec 16 '14 at 12:38

0 Answers0