Android 本應用資料清除管理器DataCleanManager

來源:互聯網
上載者:User

標籤:com   通過   name   子目錄   efi   cache   view   2.4   equals   

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.清除自訂路徑下的檔案

  

  這裡事Crowdsourced Security Testing道某個檔案,即可調用刪除方法,但是只支援目錄下的檔案刪除。

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.