簡體   English   中英

Android臨時文件-生命周期

[英]Android Temporary file - Life cycle

Android中臨時文件的生命周期是什么?

誰負責清理?

最終,我發現自己必須自己做,因此每次啟動應用程序時,我都會運行這種清除緩存的方法。

private static final long MIN_FREE_BYTES = 1024 * 1024 * 5; // this is 5M, Android recommend that there will always remind at last 1M
    public static void cleareCache(File directory){
        if(directory == null){
            directory = AppGenManager.getInstance().getCacheDir();
        }
        if(checkRemindingSpace(directory.getPath()) > MIN_FREE_BYTES){
            return;
        }

        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files){
                if(file.isDirectory()){
                    cleareCache(file);
                }
                else{
                    file.delete();
                }
            }
        }
    }

    /**
     * @param path file path
     * @return the number of available bytes on this directory
     */
    private static long checkRemindingSpace(String path){
        StatFs stat = new StatFs(path);
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM