Android realizes the function of clearing application cache
- 2021-11-13 18:20:24
- OfStack
In this article, we share the specific code of Android to clear the application cache for your reference. The specific contents are as follows
import android.content.Context;
import android.os.Environment;
import java.io.File;
import java.math.BigDecimal;
/**
* Get the cache size and clean the cache
*/
public class DataCleanManagerUtils {
/**
* Context.getExternalFilesDir() --> SDCard/Android/data/ Package name of your application /files/ Directory, 1 General release 1 Some data stored for a long time
* Context.getExternalCacheDir() --> SDCard/Android/data/ Your application package name /cache/ Directory, 1 Store temporary cache data in general
*/
/**
* Get cached value
*/
public static String getTotalCacheSize(Context context) {
long cacheSize = getFolderSize(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cacheSize += getFolderSize(context.getExternalCacheDir());
}
return getFormatSize(cacheSize);
}
/**
* Clear all caches
*/
public static void clearAllCache(Context context) {
deleteDir(context.getCacheDir());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
deleteDir(context.getExternalCacheDir());
//TODO Pay attention to troubleshooting when there is a web page cleaning, whether it exists /data/data/ Application package Can't be found under the directory database Problems with folders
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
}
}
/**
* Delete a file
*/
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();
}
if (dir != null) {
return dir.delete();
} else {
return false;
}
}
/**
* Get a file
*/
public static long getFolderSize(File file) {
long size = 0;
if (file != null) {
File[] fileList = file.listFiles();
if (fileList != null && fileList.length > 0) {
for (int i = 0; i < fileList.length; i++) {
// If there are files below
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);
} else {
size = size + fileList[i].length();
}
}
}
}
return size;
}
/**
* Formatting unit
*/
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
double megaByte = kiloByte / 1024;
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 = BigDecimal.valueOf(teraBytes);
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
+ "TB";
}
}