清除緩存功能算是個十分雞肋的功能了,但是大多數產品或者客戶都希望有這么個東西顯得APP功能完善,網上有很多,但是對于新手來說,那些感覺并不詳細,我貼個完整到小白都能懂的。
下面是工具類,包含清除緩存、獲取緩存文件大小、格式化方法。
總之就是工具,自己創建一個帖進去。
public class CacheDataManager {
public static String getTotalCacheSize(Context context) throws Exception {
long cacheSize = getFolderSize(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cacheSize += getFolderSize(context.getExternalCacheDir());
}
return getFormatSize(cacheSize);
}
public static void clearAllCache(Context context) {
deleteDir(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
deleteDir(context.getExternalCacheDir());
}
}
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
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 {
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 size
*/
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";
}
}
在你要顯示的TextView上顯示清理前的緩存大小,在Activity的onCreate()方法中直接設置就好了,其實就是調用了上面工具類的getTotalCacheSize()方法,有異常,需要捕獲。
try {
txtCacheSize.setText(CacheDataManager.getTotalCacheSize(this));
} catch (Exception e) {
e.printStackTrace();
}
再創建一個內部類,用于清理內存,實現了一個Runnable,清理完后發一個消息,這里可以靈活一點。
class clearCache implements Runnable {
@Override
public void run() {
try {
CacheDataManager.clearAllCache(SettingsActivity.this);
Thread.sleep(3000);
if (CacheDataManager.getTotalCacheSize(SettingsActivity.this).startsWith("0")) {
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
return;
}
}
}
創建一個Handle接收消息,處理結果,其實用意是清理完了就彈一個吐司,清理完成,就是這樣,也可以創建一個dialog,開始清理的時候顯示,在下面方法關閉,然后再設置一遍TextView,就是下面這樣。
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
Toast.makeText(SettingActivity.this,"清理完成",Toast.LENGTH_SHORT).show();
try {
txtCacheSize.setText(CacheDataManager.getTotalCacheSize(SettingsActivity.this));
} catch (Exception e) {
e.printStackTrace();
}
}
};
};
最后就是在設置監聽的按鈕中調用就好了。
newThread(newclearCache()).start();