致谢:感谢各位大神以及梁神
public class CleanCacheUtil {
private Context mContext;
private static CleanCacheUtil cleanCacheUtil;
private CleanCacheUtil(Context mContext) {
this.mContext = mContext;
}
public static CleanCacheUtil getInstance(Context mContext) {
if (cleanCacheUtil == null) {
cleanCacheUtil = new CleanCacheUtil(mContext);
}
return cleanCacheUtil;
}
public String countSize() {
// 计算缓存大小
long fileSize = 0;
String cacheSize = "0KB";
File cacheDir = mContext.getCacheDir();// /data/data/package_name/cache
fileSize += getDirSize(cacheDir);
File externalCacheDir = getExternalCacheDir(mContext);//"<sdcard>/Android/data/<package_name>/cache/"
fileSize += getDirSize(externalCacheDir);
fileSize -= 1024 * 10;//因为总是清不干净,所以减去一点
if (fileSize > 0)
cacheSize = formatFileSize(fileSize);
return cacheSize;
}
/**
* 获取目录文件大小
*
* @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 VersionCode
* @return
*/
public static boolean isMethodsCompat(int VersionCode) {
int currentVersion = android.os.Build.VERSION.SDK_INT;
return currentVersion >= VersionCode;
}
public static File getExternalCacheDir(Context context) {
// return context.getExternalCacheDir(); API level 8
// e.g. "<sdcard>/Android/data/<package_name>/cache/"
return context.getExternalCacheDir();
}
/**
* 转换文件大小
*
* @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;
}
//在项目中经常会使用到WebView 控件,当加载html 页面时,会在/data/data/package_name目录下生成database与cache 两个文件夹。请求的url 记录是保存在WebViewCache.db,而url 的内容是保存在WebViewCache 文件夹下
/**
* 清除app缓存
*/
public void clearAppCache() {
//先删除WebViewCache目录下的文件
mContext.deleteDatabase("webview.db");
mContext.deleteDatabase("webview.db-shm");
mContext.deleteDatabase("webview.db-wal");
mContext.deleteDatabase("webviewCache.db");
mContext.deleteDatabase("webviewCache.db-shm");
mContext.deleteDatabase("webviewCache.db-wal");
//清除数据缓存
// clearCacheFolder(mContext.getFilesDir(), System.currentTimeMillis());
clearCacheFolder(mContext.getCacheDir(), System.currentTimeMillis());
String cacheDirPath = mContext.getFilesDir().getAbsolutePath()
+ "/webcache";
clearCacheFolder(new File(cacheDirPath), System.currentTimeMillis());
//2.2版本才有将应用缓存转移到sd卡的功能
clearCacheFolder(getExternalCacheDir(mContext), System.currentTimeMillis());
// AdsUtil.clearinfo(mContext);
}
/**
* 清除缓存目录
*
* @param dir 目录
* @param curTime 当前系统时间
* @return
*/
private int clearCacheFolder(File dir, long curTime) {
int deletedFiles = 0;
if (dir != null && dir.isDirectory()) {
try {
for (File child : dir.listFiles()) {
if (child.isDirectory()) {
deletedFiles += clearCacheFolder(child, curTime);
}
if (child.lastModified() < curTime) {
if (child.delete()) {
deletedFiles++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return deletedFiles;
}
}