简体   繁体   English

如何每隔X分钟清除一次应用缓存?

[英]How do I clear app cache every X minutes?

I have an app that uses webview and utilizes cache. 我有一个使用webview并利用缓存的应用程序。 But I need to clear that cache every X minutes even if the app isn't runnung. 但我需要每隔X分钟清除一次缓存,即使该应用未运行也是如此。 How would I do that? 我该怎么做?

I've enabled cache like this: 我启用了这样的缓存:

        myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        myWebView.getSettings().setAppCacheEnabled(true);

I use this statement to delete cache with a help of a button: 我使用此语句在一个按钮的帮助下删除缓存:

myWebView.clearCache(true);

The edited code snippet above posted by Gaunt Face contains an error in that if a directory fails to delete because one of its files cannot be deleted, the code will keep retrying in an infinite loop. Gaunt Face发布的上述经过编辑的代码段包含一个错误,即如果由于无法删除其目录之一而无法删除目录,则该代码将无限循环地重试。 I rewrote it to be truly recursive, and added a numDays parameter so you can control how old the files must be that are pruned: 我将其重写为真正的递归,并添加了numDays参数,以便您可以控制修剪的文件必须多大:

//helper method for clearCache() , recursive
//returns number of deleted files
static int clearCacheFolder(final File dir, final int numDays) {

int deletedFiles = 0;
if (dir!= null && dir.isDirectory()) {
    try {
        for (File child:dir.listFiles()) {

            //first delete subdirectories recursively
            if (child.isDirectory()) {
                deletedFiles += clearCacheFolder(child, numDays);
            }

            //then delete the files and subdirectories in this dir
            //only empty directories can be deleted, so subdirs have been done first
            if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                if (child.delete()) {
                    deletedFiles++;
                }
            }
        }
    }
    catch(Exception e) {
        Log.e(TAG, String.format("Failed to clean the cache, error %s", e.getMessage()));
    }
}
return deletedFiles;
}

/*
 * Delete the files older than numDays days from the application cache
 * 0 means all files.
 */
 public static void clearCache(final Context context, final int numDays) {
Log.i(TAG, String.format("Starting cache prune, deleting files older than %d days", numDays));
int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);
Log.i(TAG, String.format("Cache pruning completed, %d files deleted", numDeletedFiles));
}
 public static void deleteDirectoryTree(File fileOrDirectory) {
        if (fileOrDirectory.isDirectory()) {
            for (File child : fileOrDirectory.listFiles()) {
                deleteDirectoryTree(child);
            }
        }

        fileOrDirectory.delete();
    }


deleteDirectoryTree(this.getCacheDir());

You can setup a timer in the Activity and after a specified timeout, call the deleteDirectoryTree 您可以在“活动”中设置一个计时器,并在指定的超时后调用deleteDirectoryTree

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM