Android内存清理

时间:2023-07-15 13:26:38

直接上图吧!Android内存清理

获取文件大小 ,清理文件工具类

public class DataCleanManager {

    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
* @return
*/
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
if (kiloByte < 1) {
// return size + "Byte";
return "0K";
} 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";
}

}

activity 中代码

//------****** 清理缓存相关****----------
private final int CLEAN_SUC=1001;
private final int CLEAN_FAIL=1002;
private void Clear() {
getConfirmDialog(this, "是否清空缓存?", new DialogInterface.OnClickListener
() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
clearAppCache();
hcsize.setText("0KB");
}
}).show();
}
public static AlertDialog.Builder getConfirmDialog(Context context, String message, DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = getDialog(context);
builder.setMessage(Html.fromHtml(message));
builder.setPositiveButton("确定", onClickListener);
builder.setNegativeButton("取消", null);
return builder;
}
public static AlertDialog.Builder getDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
return builder;
} /**
* 清除app缓存
*/
public void myclearaAppCache() { // 清除数据缓存
DataCleanManager.clearAllCache(this);
// 2.2版本才有将应用缓存转移到sd卡的功能
// 清除编辑器保存的临时内容
Properties props = getProperties();
for (Object key : props.keySet()) {
String _key = key.toString();
if (_key.startsWith("temp"))
removeProperty(_key);
} KLog.e("清除缓存");
// Core.getKJBitmap().cleanCache(); } /**
* 清除保存的缓存
*/
public Properties getProperties() {
return AppConfig.getAppConfig(this).get();
}
public void removeProperty(String... key) {
AppConfig.getAppConfig(this).remove(key);
}
/**
* 清除app缓存
*
* @param
*/
public void clearAppCache() { new Thread() {
@Override
public void run() {
Message msg = new Message();
try {
myclearaAppCache();
msg.what = CLEAN_SUC;
} catch (Exception e) {
e.printStackTrace();
msg.what = CLEAN_FAIL;
}
handler.sendMessage(msg);
}
}.start();
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case CLEAN_FAIL:
Toast.makeText(MySetClass.this,"清除失败",Toast.LENGTH_LONG).show();
break;
case CLEAN_SUC: Toast.makeText(MySetClass.this,"清除成功",Toast.LENGTH_LONG).show();
break;
}
};
}; 运行结果如下:

Android内存清理