Android清空缓存

/**
    * @param context
    *            删除缓存
    */
   public static void clearAllCache(Context context) {
       deleteDir(context.getCacheDir());
       if (Environment.getExternalStorageState().equals(
               Environment.MEDIA_MOUNTED)) {
           deleteDir(context.getExternalCacheDir());
       }
   }

   private static boolean deleteDir(File dir) {
       if (dir != null && dir.isDirectory()) {
           String[] children = dir.list();
           int size = 0;
           if (children != null) {
               size = children.length;
               for (int i = 0; i < size; i++) {
                   boolean success = deleteDir(new File(dir, children[i]));
                   if (!success) {
                       return false;
                   }
               }
           }

       }
       if (dir == null) {
           return true;
       } else {

           return dir.delete();
       }
   }



猜你喜欢

转载自blog.csdn.net/e_d_i_e/article/details/79078515