2013-03-05 68 views
0

嗨朋友感谢以前的回复, 我面临删除缓存和临时文件/文件夹的问题, 我需要的是从一个应用程序清理整个设备临时文件和缓存是我的应用程序 但在这里我能够只清洁我的应用程序缓存,这里是我的代码缓存和临时文件/文件夹删除android

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()); 

    } 

和我清除缓存的方法下

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

请帮助我怎样才能清除整个装置缓存,这里我是ge拟合每Apps缓存位置即DIR在设备中的所有应用程序的缓存,但是当我要删除它们返回false

请帮助任何帮助是明显的

我能够清除一个应用程序的缓存也就是一个我运行这段代码,但不能用于其他应用提前

感谢

回答

0

尝试使用此代码

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

这将d删除所有数据,并在应用程序中缓存文件。

+0

普通的SDK应用程序没有权限访问,更不用说修改其他应用程序的缓存了,而不仅仅是他们有权破解您的文件。 对于以root身份运行的应用程序,这可能是可行的,在这种情况下,您将不得不根据应用程序的软件包名称手动构建路径。 http://stackoverflow.com/questions/4605571/clearing-app-cache-programmatically – jithu 2013-03-05 10:40:34

+0

如果它是可能的根源手机然后如何Android的商店有缓存清洁的应用程序,以及如何能够安装和这些很好地工作 请帮助如果有什么 – amit 2013-03-11 11:33:33

+0

,并感谢您的朋友prev回复 – amit 2013-03-11 11:33:51