DataCleanManager 内存管理器

  1. package com.infzm.daily.know.utils;  
  2.   
  3. /*  * 文 件 名:  DataCleanManager.java   
  4.  * * 描    述:  主要功能有清除内/外缓存,清除数据库,清除sharedPreference,清除files和清除自定义目录   
  5.  * */  
  6.   
  7. import java.io.File;  
  8. import java.math.BigDecimal;  
  9.   
  10. import android.content.Context;  
  11. import android.os.Environment;  
  12. import android.text.TextUtils;  
  13.   
  14. /** * 本应用数据清除管理器 */  
  15. public class DataCleanManager {  
  16.     /** 
  17.      * * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * 
  18.      *  
  19.      * @param context 
  20.      */  
  21.     public static void cleanInternalCache(Context context) {  
  22.         deleteFilesByDirectory(context.getCacheDir());  
  23.     }  
  24.   
  25.     /** 
  26.      * * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * 
  27.      *  
  28.      * @param context 
  29.      */  
  30.     public static void cleanDatabases(Context context) {  
  31.         deleteFilesByDirectory(new File("/data/data/"  
  32.                 + context.getPackageName() + "/databases"));  
  33.     }  
  34.   
  35.     /** 
  36.      * * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * 
  37.      *  
  38.      * @param context 
  39.      */  
  40.     public static void cleanSharedPreference(Context context) {  
  41.         deleteFilesByDirectory(new File("/data/data/"  
  42.                 + context.getPackageName() + "/shared_prefs"));  
  43.     }  
  44.   
  45.     /** 
  46.      * * 按名字清除本应用数据库 * * 
  47.      *  
  48.      * @param context 
  49.      * @param dbName 
  50.      */  
  51.     public static void cleanDatabaseByName(Context context, String dbName) {  
  52.         context.deleteDatabase(dbName);  
  53.     }  
  54.   
  55.     /** 
  56.      * * 清除/data/data/com.xxx.xxx/files下的内容 * * 
  57.      *  
  58.      * @param context 
  59.      */  
  60.     public static void cleanFiles(Context context) {  
  61.         deleteFilesByDirectory(context.getFilesDir());  
  62.     }  
  63.   
  64.     /** 
  65.      * * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) 
  66.      *  
  67.      * @param context 
  68.      */  
  69.     public static void cleanExternalCache(Context context) {  
  70.         if (Environment.getExternalStorageState().equals(  
  71.                 Environment.MEDIA_MOUNTED)) {  
  72.             deleteFilesByDirectory(context.getExternalCacheDir());  
  73.         }  
  74.     }  
  75.     /** 
  76.      * * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除 * * 
  77.      *  
  78.      * @param filePath 
  79.      * */  
  80.     public static void cleanCustomCache(String filePath) {  
  81.         deleteFilesByDirectory(new File(filePath));  
  82.     }  
  83.   
  84.     /** 
  85.      * * 清除本应用所有的数据 * * 
  86.      *  
  87.      * @param context 
  88.      * @param filepath 
  89.      */  
  90.     public static void cleanApplicationData(Context context, String... filepath) {  
  91.         cleanInternalCache(context);  
  92.         cleanExternalCache(context);  
  93.         cleanDatabases(context);  
  94.         cleanSharedPreference(context);  
  95.         cleanFiles(context);  
  96.         if (filepath == null) {  
  97.             return;  
  98.         }  
  99.         for (String filePath : filepath) {  
  100.             cleanCustomCache(filePath);  
  101.         }  
  102.     }  
  103.   
  104.     /** 
  105.      * * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 * * 
  106.      *  
  107.      * @param directory 
  108.      */  
  109.     private static void deleteFilesByDirectory(File directory) {  
  110.         if (directory != null && directory.exists() && directory.isDirectory()) {  
  111.             for (File item : directory.listFiles()) {  
  112.                 item.delete();  
  113.             }  
  114.         }  
  115.     }  
  116.       
  117.     // 获取文件  
  118.     //Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据  
  119.     //Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据  
  120.     public static long getFolderSize(File file) throws Exception {  
  121.         long size = 0;  
  122.         try {  
  123.             File[] fileList = file.listFiles();  
  124.             for (int i = 0; i < fileList.length; i++) {  
  125.                 // 如果下面还有文件  
  126.                 if (fileList[i].isDirectory()) {  
  127.                     size = size + getFolderSize(fileList[i]);  
  128.                 } else {  
  129.                     size = size + fileList[i].length();  
  130.                 }  
  131.             }  
  132.         } catch (Exception e) {  
  133.             e.printStackTrace();  
  134.         }  
  135.         return size;  
  136.     }  
  137.       
  138.     /** 
  139.      * 删除指定目录下文件及目录 
  140.      *  
  141.      * @param deleteThisPath 
  142.      * @param filepath 
  143.      * @return 
  144.      */  
  145.     public static void deleteFolderFile(String filePath, boolean deleteThisPath) {  
  146.         if (!TextUtils.isEmpty(filePath)) {  
  147.             try {  
  148.                 File file = new File(filePath);  
  149.                 if (file.isDirectory()) {// 如果下面还有文件  
  150.                     File files[] = file.listFiles();  
  151.                     for (int i = 0; i < files.length; i++) {  
  152.                         deleteFolderFile(files[i].getAbsolutePath(), true);  
  153.                     }  
  154.                 }  
  155.                 if (deleteThisPath) {  
  156.                     if (!file.isDirectory()) {// 如果是文件,删除  
  157.                         file.delete();  
  158.                     } else {// 目录  
  159.                         if (file.listFiles().length == 0) {// 目录下没有文件或者目录,删除  
  160.                             file.delete();  
  161.                         }  
  162.                     }  
  163.                 }  
  164.             } catch (Exception e) {  
  165.                 // TODO Auto-generated catch block  
  166.                 e.printStackTrace();  
  167.             }  
  168.         }  
  169.     }  
  170.       
  171.     /** 
  172.      * 格式化单位 
  173.      *  
  174.      * @param size 
  175.      * @return 
  176.      */  
  177.     public static String getFormatSize(double size) {  
  178.         double kiloByte = size / 1024;  
  179.         if (kiloByte < 1) {  
  180.             return size + "Byte";  
  181.         }  
  182.   
  183.         double megaByte = kiloByte / 1024;  
  184.         if (megaByte < 1) {  
  185.             BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));  
  186.             return result1.setScale(2, BigDecimal.ROUND_HALF_UP)  
  187.                     .toPlainString() + "KB";  
  188.         }  
  189.   
  190.         double gigaByte = megaByte / 1024;  
  191.         if (gigaByte < 1) {  
  192.             BigDecimal result2 = new BigDecimal(Double.toString(megaByte));  
  193.             return result2.setScale(2, BigDecimal.ROUND_HALF_UP)  
  194.                     .toPlainString() + "MB";  
  195.         }  
  196.   
  197.         double teraBytes = gigaByte / 1024;  
  198.         if (teraBytes < 1) {  
  199.             BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));  
  200.             return result3.setScale(2, BigDecimal.ROUND_HALF_UP)  
  201.                     .toPlainString() + "GB";  
  202.         }  
  203.         BigDecimal result4 = new BigDecimal(teraBytes);  
  204.         return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()  
  205.                 + "TB";  
  206.     }  
  207.       
  208.       
  209.     public static String getCacheSize(File file) throws Exception {  
  210.         return getFormatSize(getFolderSize(file));  
  211.     }  
  212.       
  213. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值