zoukankan      html  css  js  c++  java
  • Android 本应用数据清除管理器DataCleanManager

    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.点击了之后清除缓存

      

      这里给出了一个指定目录,而且不删除路径。 


    既然选择了,便不顾风雨兼程。Just follow yourself.
  • 相关阅读:
    火车头采集标题如何伪原创(附教程)
    神经网络的模型分为哪几类,神经网络训练模型描述
    CSS background-color 详细说明 ccs修改背景颜色
    三大神经网络模型,神经网络简单实例
    golang查看数据类型
    git根据某个commit创建分支
    RocketMQ在在CentOS7中使用systemctl方式启动
    CentOS7使用firewall-cmd打开关闭防火墙与端口
    1,6,18,30,75的规律php版本
    发现自己
  • 原文地址:https://www.cnblogs.com/Jason-Jan/p/7908477.html
Copyright © 2011-2022 走看看