1. 程式人生 > >Android 本應用數據清除管理器DataCleanManager

Android 本應用數據清除管理器DataCleanManager

sdi body format except [] bsp 參數 base adding

1.整體分析

1.1.源代碼先給出了,可以直接Copy。

技術分享圖片
/**
 * 本應用數據清除管理器
 */
public class DataCleanManager {
    /**
     * * 清除本應用內部緩存(/data/data/com.xxx.xxx/cache) * *
     *
     * @param context
     */
    public static void cleanInternalCache(Context context) {
        deleteFilesByDirectory(context.getCacheDir());
    }

    
/** * * 清除本應用所有數據庫(/data/data/com.xxx.xxx/databases) * * * * @param context */ public static void cleanDatabases(Context context) { deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/databases")); } /** * * 清除本應用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * *
@param context */ public static void cleanSharedPreference(Context context) { deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/shared_prefs")); } /** * * 按名字清除本應用數據庫 * * * * @param context * @param dbName
*/ public static void cleanDatabaseByName(Context context, String dbName) { context.deleteDatabase(dbName); } /** * * 清除/data/data/com.xxx.xxx/files下的內容 * * * * @param context */ public static void cleanFiles(Context context) { deleteFilesByDirectory(context.getFilesDir()); } /** * * 清除外部cache下的內容(/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param context */ public static void cleanExternalCache(Context context) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { deleteFilesByDirectory(context.getExternalCacheDir()); } } /** * * 清除自定義路徑下的文件,使用需小心,請不要誤刪。而且只支持目錄下的文件刪除 * * * * @param filePath */ public static void cleanCustomCache(String filePath) { deleteFilesByDirectory(new File(filePath)); } /** * * 清除本應用所有的數據 * * * * @param context * @param filepath */ public static void cleanApplicationData(Context context, String... filepath) { cleanInternalCache(context); cleanExternalCache(context); cleanDatabases(context); cleanSharedPreference(context); cleanFiles(context); if (filepath == null) { return; } for (String filePath : filepath) { cleanCustomCache(filePath); } } /** * * 刪除方法 這裏只會刪除某個文件夾下的文件,如果傳入的directory是個文件,將不做處理 * * * * @param directory */ private static void deleteFilesByDirectory(File directory) { if (directory != null && directory.exists() && directory.isDirectory()) { for (File item : directory.listFiles()) { item.delete(); } } } // 獲取文件 //Context.getExternalFilesDir() --> SDCard/Android/data/你的應用的包名/files/ 目錄,一般放一些長時間保存的數據 //Context.getExternalCacheDir() --> SDCard/Android/data/你的應用包名/cache/目錄,一般存放臨時緩存數據 public static long getFolderSize(File file) throws Exception { long size = 0; try { File[] fileList = file.listFiles(); for (int i = 0; i < fileList.length; i++) { // 如果下面還有文件 if (fileList[i].isDirectory()) { size = size + getFolderSize(fileList[i]); } else { size = size + fileList[i].length(); } } } catch (Exception e) { e.printStackTrace(); } return size; } /** * 刪除指定目錄下文件及目錄 * * @param deleteThisPath * @param filePath * @return */ public static void deleteFolderFile(String filePath, boolean deleteThisPath) { if (!TextUtils.isEmpty(filePath)) { try { File file = new File(filePath); if (file.isDirectory()) {// 如果下面還有文件 File files[] = file.listFiles(); for (int i = 0; i < files.length; i++) { deleteFolderFile(files[i].getAbsolutePath(), true); } } if (deleteThisPath) { if (!file.isDirectory()) {// 如果是文件,刪除 file.delete(); } else {// 目錄 if (file.listFiles().length == 0) {// 目錄下沒有文件或者目錄,刪除 file.delete(); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 格式化單位 * * @param size * @return */ public static String getFormatSize(double size) { double kiloByte = size / 1024; if (kiloByte < 1) { return size + "Byte"; } double megaByte = kiloByte / 1024; if (megaByte < 1) { BigDecimal result1 = new BigDecimal(Double.toString(kiloByte)); return result1.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "KB"; } double gigaByte = megaByte / 1024; if (gigaByte < 1) { BigDecimal result2 = new BigDecimal(Double.toString(megaByte)); return result2.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "MB"; } double teraBytes = gigaByte / 1024; if (teraBytes < 1) { BigDecimal result3 = new BigDecimal(Double.toString(gigaByte)); return result3.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "GB"; } BigDecimal result4 = new BigDecimal(teraBytes); return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB"; } public static String getCacheSize(File file) throws Exception { return getFormatSize(getFolderSize(file)); } }
View Code

1.2.主要功能

  清除內/外緩存,清除數據庫,清除SharePreference,清除文件,刪除文件,格式化單位等。

  

1.3.方法列表

  • cleanInternalCache(Context)==>清除本應用內部緩存
  • cleanDatabases(Context)==>清除本應用所有數據庫
  • cleanSharePreference(Context)==>清除本應用SharePreference
  • cleanDatabaseByName(Context,String)==>按名字清除本應用數據庫
  • cleanFiles(Context)==>清除files下的內容
  • cleanExternalCache(Context)==>清除外部緩存下的內容
  • cleanCustomCache(String)==>清除自定義路徑下的文件
  • cleanApplicationData(Context context,String... filepath)==>清除本應用所有的數據 
  • deleteFilesByDirectory(File)==>刪除某個文件夾下的文件
  • getFolderSize(File)==>獲取當前文件夾的大小,包括文件夾中的文件夾。
  • deleteFolderFile(String,boolean)==>刪除指定目錄下下的文件及目錄
  • getFormatSize(double)==>格式化單位
  • getCacheSize(File)==>獲取緩存文件的大小 


2.局部分析

2.1.清除本應用內部緩存

  技術分享圖片

  傳入一個上下文,可以獲取內部緩存路徑,然後調用刪除方法

  技術分享圖片

2.2.清除本應用所有數據庫

  技術分享圖片

  然後同樣調用了delete方法。

  這裏約定好數據庫的文件路徑有databases。

2.3.清除本應用SharePreference

  技術分享圖片

  同樣調用了刪除方法。

  這裏約定好數據庫文件路徑有shared_prefs。

2.4.按名字清除本應用數據庫

  技術分享圖片

  這裏調用了context中的方法,可以直接清除數據庫。

2.5.清除本應用下files下的文件

  技術分享圖片

  這裏先通過context獲取本應用files的地址,然後調用刪除方法。

2.6.清除外部緩存下的文件

  技術分享圖片

  這裏了先判斷是否有外部緩存,然後通過context獲取外部緩存地址,然後調用刪除方法。

2.7.清除自定義路徑下的文件

  技術分享圖片

  這裏事先知道某個文件,即可調用刪除方法,但是只支持目錄下的文件刪除。

2.8.清除本應用所有的數據

  技術分享圖片

  這裏就是調用了上面所有的方法。

2.9.獲取文件夾的大小(裏面可能還有子目錄)

  技術分享圖片

  遍歷每一個文件,獲取size大小之和。

2.10.刪除指定目錄下文件及目錄

  技術分享圖片

  給一個指定目錄,然後後面那個參數有點多余了。利用file.delete()即可刪除文件。

2.11.格式化單位

  技術分享圖片

  給定一個size,然後計算成合理的單位。

2.12.獲取緩存大小

  技術分享圖片

  得到一個file參數,判斷文件夾的大小,然後再格式化單位,從而知道了緩存大小。


3.用法實例

3.1.比如在一個設置的活動

  要知道緩存的大小。

  技術分享圖片

  調用了DataCleanManager的一個getCacheSize函數,事先通過FileUtil獲取外部緩存地址。

3.2.點擊了之後清除緩存

  技術分享圖片

  這裏給出了一個指定目錄,而且不刪除路徑。


Android 本應用數據清除管理器DataCleanManager