Android清除快取和資料庫等資料
阿新 • • 發佈:2019-01-25
import android.os.Environment; import java.io.File; import java.math.BigDecimal; /** * 清除應用快取 */ public class ClearData { private Context context; public ClearData(Context context){ this.context = context; } /** * 清除應用所有的資料 */ public void clearAllDataOfApplication( String... customfilepath) { clearInternalCache(); clearExternalCache(); clearDatabases(); clearSharedPreference(); clearFiles(); if (customfilepath == null) { return; } for (String filePath : customfilepath) { clearCustomCache(filePath); } } /* *清除外部和內部快取 */ public void clearAllCache() { clearExternalCache(); clearInternalCache(); } /** * 清除內部快取(/data/data/包名/cache) */ public void clearInternalCache() { deleteFilesByDirectory(context.getCacheDir()); } /** * 清除應用SharedPreference(/data/data/包名/shared_prefs) */ public void clearSharedPreference() { deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/shared_prefs")); } /** * *清除所有資料庫(/data/data/包名/databases) */ public void clearDatabases() { deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/databases")); } /** * 按名字清除資料庫 */ public void clearDatabaseBydbName(String dbName) { context.deleteDatabase(dbName); } /** * 清除/data/data/包名/files下的內容 */ public void clearFiles() { deleteFilesByDirectory(context.getFilesDir()); } /** * 清除自定義路徑下的檔案(只支援目錄下的檔案刪除,不支援目錄) */ public void clearCustomCache(String filePath) { deleteFilesByDirectory(new File(filePath)); } /** * 清除外部SDcard下的cache內容(/mnt/sdcard/android/data/包名/cache) */ public void clearExternalCache() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { deleteFilesByDirectory(context.getExternalCacheDir()); } } /** * 只刪除資料夾下的檔案 * */ private static void deleteFilesByDirectory(File directory) { if (directory != null && directory.exists() && directory.isDirectory()) { for (File item : directory.listFiles()) { item.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; } /** * 獲取應用檔案快取大小 */ public String getCacheSize(File file) throws Exception { return getFormatSize(getFolderSize(file)); } /** * 獲取外部和內部快取大小 */ public String getTotalCacheSize() throws Exception { long cacheSize = getFolderSize(context.getCacheDir()); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { cacheSize += getFolderSize(context.getExternalCacheDir()); } return getFormatSize(cacheSize); } /** * 格式化快取單位 */ public 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"; } }