清除缓存功能算是个十分鸡肋的功能了,但是大多数产品或者客户都希望有这么个东西显得APP功能完善,网上有很多,但是对于新手来说,那些感觉并不详细,我贴个完整到小白都能懂的。
下面是工具类,包含清除缓存、获取缓存文件大小、格式化方法。
总之就是工具,自己创建一个帖进去。
public class CacheDataManager {undefined
public static String getTotalCacheSize(Context context) throws Exception {undefined
long cacheSize = getFolderSize(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {undefined
cacheSize += getFolderSize(context.getExternalCacheDir());
}
return getFormatSize(cacheSize);
}
public static void clearAllCache(Context context) {undefined
deleteDir(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {undefined
deleteDir(context.getExternalCacheDir());
}
}
private static boolean deleteDir(File dir) {undefined
if (dir != null && dir.isDirectory()) {undefined
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {undefined
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {undefined
return false;
}
}
}
return dir.delete();
}
// 获取文件
// Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/
// 目录,一般放一些长时间保存的数据
// Context.getExternalCacheDir() -->
// SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据
public static long getFolderSize(File file) throws Exception {undefined
long size = 0;
try {undefined
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {undefined
// 如果下面还有文件
if (fileList[i].isDirectory()) {undefined
size = size + getFolderSize(fileList[i]);
} else {undefined
size = size + fileList[i].length();
}
}
} catch (Exception e) {undefined
e.printStackTrace();
}
return size;
}
/**
* 格式化单位
*
* @param size
*/
public static String getFormatSize(double size) {undefined
double kiloByte = size / 1024;
if (kiloByte < 1) {undefined
return size + "Byte";
}
double megaByte = kiloByte / 1024;
if (megaByte < 1) {undefined
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
}
double gigaByte = megaByte / 1024;
if (gigaByte < 1) {undefined
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
}
double teraBytes = gigaByte / 1024;
if (teraBytes < 1) {undefined
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";
}
}
在你要显示的TextView上显示清理前的缓存大小,在Activity的onCreate()方法中直接设置就好了,其实就是调用了上面工具类的getTotalCacheSize()方法,有异常,需要捕获。
try {undefined
txtCacheSize.setText(CacheDataManager.getTotalCacheSize(this));
} catch (Exception e) {undefined
e.printStackTrace();
}
再创建一个内部类,用于清理内存,实现了一个Runnable,清理完后发一个消息,这里可以灵活一点。
class clearCache implements Runnable {
@Override
public void run() {undefined
try {undefined
CacheDataManager.clearAllCache(SettingsActivity.this);
Thread.sleep(3000);
if (CacheDataManager.getTotalCacheSize(SettingsActivity.this).startsWith("0")) {undefined
handler.sendEmptyMessage(0);
}
} catch (Exception e) {undefined
return;
}
}
}
创建一个Handle接收消息,处理结果,其实用意是清理完了就弹一个吐司,清理完成,就是这样,也可以创建一个dialog,开始清理的时候显示,在下面方法关闭,然后再设置一遍TextView,就是下面这样。
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {undefined
switch (msg.what) {undefined
case 0:
Toast.makeText(SettingActivity.this,"清理完成",Toast.LENGTH_SHORT).show();
try {undefined
txtCacheSize.setText(CacheDataManager.getTotalCacheSize(SettingsActivity.this));
} catch (Exception e) {undefined
e.printStackTrace();
}
}
};
};