Android中清除缓存模块
来源:互联网 发布:罗辑思维的骗局 知乎 编辑:程序博客网 时间:2024/05/17 23:28
Android中清除缓存以及本地存储文件
创建一个清除缓存的工具类
public class DataCleanUtils { /** * 删除文件 * @param context */ public static void cleanPath(Context context) { deleteFilesByDirectory(new File(context.getPackageCodePath())); deleteFilesByDirectory(new File(context.getCacheDir().getPath())); deleteFilesByDirectory(new File(context.getExternalCacheDir().getPath())); deleteFilesByDirectory(new File(context.getFilesDir().getPath())); deleteFilesByDirectory(new File(context.getObbDir().getPath())); deleteFilesByDirectory(new File(context.getPackageResourcePath())); cleanSharedPreference(context); } /** * 获取删除的文件大小 * @param context * @return */ public static String getDeleteSiZe(Context context){ return formatFileSize(allSize(context)); } public static long allSize(Context context) { return getDirSize(new File(context.getPackageCodePath())) + getDirSize(new File(context.getCacheDir().getPath())) + getDirSize(new File(context.getExternalCacheDir().getPath())) + getDirSize(new File(context.getFilesDir().getPath())) + getDirSize(new File(context.getObbDir().getPath())) + getDirSize(new File(context.getPackageResourcePath())); } /** * * 清除本应用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 dir * @return */ public static long getDirSize(File dir) { if (dir == null) { return 0; } if (!dir.isDirectory()) { return 0; } long dirSize = 0; File[] files = dir.listFiles(); for (File file : files) { if (file.isFile()) { dirSize += file.length(); } else if (file.isDirectory()) { dirSize += file.length(); dirSize += getDirSize(file); // 递归调用继续统计 } } return dirSize; } /** * 递归删除文件和文件夹 * * @param file 要删除的根目录 */ public static void deleteFilesByDirectory(File file) { if (file.isFile()) { file.delete(); return; } if (file.isDirectory()) { File[] childFile = file.listFiles(); if (childFile == null || childFile.length == 0) { file.delete(); return; } for (File f : childFile) { deleteFilesByDirectory(f); } file.delete(); } } /** * 转换文件大小 * * @param fileS * @return B/KB/MB/GB */ public static String formatFileSize(long fileS) { java.text.DecimalFormat df = new java.text.DecimalFormat("#.00"); String fileSizeString = ""; if (fileS < 1024) { fileSizeString = df.format((double) fileS) + "B"; } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "KB"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "MB"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "G"; } return fileSizeString; }}
工具类的使用demo(防止空间获取数据过大,放在子线程中操作)
private void cleanData() { showProgressDialog("","数据清理中",false); new Thread(new Runnable() { @Override public void run() { final long l = DataCleanUtils.allSize(getApplicationContext()); DataCleanUtils.cleanPath(getApplicationContext()); runOnUiThread(new Runnable() { @Override public void run() { if(l > DataCleanUtils.allSize(getApplicationContext())){ dismissProgressDialog(); long freeSize = l - DataCleanUtils.allSize(getApplicationContext()); Toast.makeText(MainActivity.this, "释放了"+DataCleanUtils.formatFileSize(freeSize)+"空间", Toast.LENGTH_LONG).show(); }else{ dismissProgressDialog(); Toast.makeText(MainActivity.this, "空间已经清理干净", Toast.LENGTH_SHORT).show(); } } }); } }).start(); dismissProgressDialog(); }
阅读全文
1 0
- Android中清除缓存模块
- android中清除缓存数据
- Android中如何清除缓存
- Android APP中清除缓存功能详解
- Android APP中清除缓存功能详解
- Android APP中清除缓存功能详解
- Android中获取缓存大小和清除缓存功能
- android清除缓存,查看缓存
- Android 清除程序缓存
- android清除缓存
- android清除缓存
- android 清除缓存功能
- android WebView清除缓存
- Android清除webview缓存
- Android 清除应用缓存
- Android清除应用缓存
- Android清除浏览器缓存
- Android 清除缓存
- Storm IRichSpout
- cad转pdf格式怎么设置线宽
- 人脸识别正则化系列之normface
- java web 学习笔记
- __cdecl与_stdcall
- Android中清除缓存模块
- 解决Redis局域网内不能访问的问题(Windows/Linux)
- kd树划分空间选择维度使用方差的好处
- HTML5的相关内容
- js判断本地文件是否存在
- 统计一个数组中多少重复的元素
- 使用InetAddress类获取本地计算机的IP地址和主机名
- 处理大数据量
- FD_ZERO,FD_SET,FD_CLR,FD_ISSET