0

Hi friend thanks for previous replies, i am facing problem in removing cache and temp files/folders, what i require is to clean the whole device temp files and cache from one app which is mine app but here i am able to clean only my apps cache , here is my code

 private void mAppMethod(List<App> mApps) {
    // TODO Auto-generated method stub

            // File f = g
    for (int i = 0; i < mApps.size(); i++) {
          File dir = new  File("/data/data/"+mApps.get(i).getPackageName().concat("/cache"));

          Log.e("dir "+dir, "is directory "+dir.isDirectory());
            int j =    clearCacheFolder(dir, 10);
            if (dir!= null && dir.isDirectory())

                Log.e("j", "rff "+dir.delete());
            System.out.println(j+" rff "+dir.delete());

    }  

and my clear cache method as under

 static int clearCacheFolder(final File dir, final int numDays) {

          int deletedFiles = 0;
          if (dir!= null && dir.isDirectory()) {
            //  System.out.println("here"+dir.delete());
              Log.e("here", "here  "+dir.isDirectory());
              try {

                  Log.e("here1", "here1"+dir.listFiles());
                  for (File child:dir.listFiles()) {
                      Log.e("here11", "here11");
                      //first delete subdirectories recursively
                      if (child.isDirectory()) {
                          Log.e("here111", "here111");
                          deletedFiles += clearCacheFolder(child, numDays);
                          Log.e("here1111", "here1111");
                      }
                      Log.e("here11111", "here11111");
                      //then delete the files and subdirectories in this dir
                      //only empty directories can be deleted, so subdirs have been done first
                      if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                           Log.e("here111111", "here111111");
                          if (child.delete()) {
                               Log.e("here1111111", "here1111111");
                              deletedFiles++;
                              Log.e("here11111111", "here11111111");
                          }
                      }
                  }
              }
              catch(Exception e) {
                  Log.e("TAG", String.format("Failed to clean the cache, error %s", e.getMessage()));
              }
          }
          return deletedFiles;
      }

please help how can i clear the whole device cache,here i am getting every apps cache location i.e. dir to cache of all apps in device but when i want to delete them it returns false

please help any help is appreciable

i am able to clear cache of one app which is the one i am running this code but not for other apps

thanks in advance

Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
amit
  • 11
  • 1
  • 5

1 Answers1

0

Try with this code

public void clearApplicationData() {
    File cache = getCacheDir();
    File appDir = new File(cache.getParent());
    if(appDir.exists()){
        String[] children = appDir.list();
        for(String s : children){
            if(!s.equals("lib")){
                deleteDir(new File(appDir, s));
                Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s +" DELETED *******************");
            }
        }
    }
}

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;
            }
        }
    }

    return dir.delete();
}

This will delete your all data,and cache files in the application.

jithu
  • 706
  • 1
  • 9
  • 13
  • Ordinary SDK applications have no rights to access, let alone modify, the caches of other applications, any more than they have a right to hack your files. This may be possible on rooted phones with your application running as root, in which case you will have to manually construct the paths based upon the apps' package names. http://stackoverflow.com/questions/4605571/clearing-app-cache-programmatically – jithu Mar 05 '13 at 10:40
  • if it is possible in rooted phones then how can android play store have cache cleaner apps and how m i able to install and those are working nicely please help if there is anything – amit Mar 11 '13 at 11:33
  • and @jithu this code will remove cache and other files of the same app on which we are working but not for whole intalled and native apps of the android phone – amit Mar 11 '13 at 11:34