現在很多APP中都有系統設定,這個子產品中有一個緩存設定功能,使用者可以檢視目前APP緩存資料大小并且可以手動清空緩存資料。
緩存資料的統計分2塊:記憶體(這裡指的是應用程式包目錄所在位置)+外存(外部存儲卡)
我這裡以開源中國APP資料緩存處理為例為大家講解下
清除的目錄包括:
1./data/data/package_name/files
2./data/data/package_name/cache
3.<sdcard>/Android/data/<package_name>/cache/
4.webview緩存資料
[plain] view plain copy print ?
- // 計算緩存大小
- long fileSize = 0;
- String cacheSize = "0KB";
- File filesDir = getFilesDir();// /data/data/package_name/files
- File cacheDir = getCacheDir();// /data/data/package_name/cache
- fileSize += getDirSize(filesDir);
- fileSize += getDirSize(cacheDir);
- // 2.2版本才有将應用緩存轉移到sd卡的功能
- if(isMethodsCompat(android.os.Build.VERSION_CODES.FROYO)){
- File externalCacheDir = getExternalCacheDir(this);//"<sdcard>/Android/data/<package_name>/cache/"
- fileSize += getDirSize(externalCacheDir);
- }
- if (fileSize > 0)
- cacheSize = formatFileSize(fileSize);
- public static long getDirSize(File dir) {
- if (dir == null) {
- return 0;
- }
- if (!dir.isDirectory()) {
- return 0;
- }
- long dirSize = 0;
- File[] files = dir.listFiles();
- for (File file : files) {
- if (file.isFile()) {
- dirSize += file.length();
- } else if (file.isDirectory()) {
- dirSize += file.length();
- dirSize += getDirSize(file); // 遞歸調用繼續統計
- }
- }
- return dirSize;
- }
- public static boolean isMethodsCompat(int VersionCode) {
- int currentVersion = android.os.Build.VERSION.SDK_INT;
- return currentVersion >= VersionCode;
- }
- @TargetApi(8)
- public static File getExternalCacheDir(Context context) {
- // return context.getExternalCacheDir(); API level 8
- // e.g. "<sdcard>/Android/data/<package_name>/cache/"
- return context.getExternalCacheDir();
- }
- public static String formatFileSize(long fileS) {
- java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
- String fileSizeString = "";
- if (fileS < 1024) {
- fileSizeString = df.format((double) fileS) + "B";
- } else if (fileS < 1048576) {
- fileSizeString = df.format((double) fileS / 1024) + "KB";
- } else if (fileS < 1073741824) {
- fileSizeString = df.format((double) fileS / 1048576) + "MB";
- } else {
- fileSizeString = df.format((double) fileS / 1073741824) + "G";
- }
- return fileSizeString;
- }
- public static void clearAppCache(Activity activity) {
- final AppContext ac = (AppContext) activity.getApplication();
- final Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- if (msg.what == 1) {
- ToastMessage(ac, "緩存清除成功");
- } else {
- ToastMessage(ac, "緩存清除失敗");
- }
- }
- };
- new Thread() {
- public void run() {
- Message msg = new Message();
- try {
- ac.clearAppCache();
- msg.what = 1;
- } catch (Exception e) {
- e.printStackTrace();
- msg.what = -1;
- }
- handler.sendMessage(msg);
- }
- }.start();
- }
- 在項目中經常會使用到WebView 控件,當加載html 頁面時,會在/data/data/package_name目錄下生成database與cache 兩個檔案夾。請求的url 記錄是儲存在WebViewCache.db,而url 的内容是儲存在WebViewCache 檔案夾下
- public void clearAppCache()
- {
- //清除webview緩存
- @SuppressWarnings("deprecation")
- File file = CacheManager.getCacheFileBaseDir();
- //先删除WebViewCache目錄下的檔案
- if (file != null && file.exists() && file.isDirectory()) {
- for (File item : file.listFiles()) {
- item.delete();
- }
- file.delete();
- }
- deleteDatabase("webview.db");
- deleteDatabase("webview.db-shm");
- deleteDatabase("webview.db-wal");
- deleteDatabase("webviewCache.db");
- deleteDatabase("webviewCache.db-shm");
- deleteDatabase("webviewCache.db-wal");
- //清除資料緩存
- clearCacheFolder(getFilesDir(),System.currentTimeMillis());
- clearCacheFolder(getCacheDir(),System.currentTimeMillis());
- //2.2版本才有将應用緩存轉移到sd卡的功能
- if(isMethodsCompat(android.os.Build.VERSION_CODES.FROYO)){
- clearCacheFolder(getExternalCacheDir(this),System.currentTimeMillis());
- }
- }
- private int clearCacheFolder(File dir, long curTime) {
- int deletedFiles = 0;
- if (dir!= null && dir.isDirectory()) {
- try {
- for (File child:dir.listFiles()) {
- if (child.isDirectory()) {
- deletedFiles += clearCacheFolder(child, curTime);
- }
- if (child.lastModified() < curTime) {
- if (child.delete()) {
- deletedFiles++;
- }
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- return deletedFiles;
- }