2013-03-05 81 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