简体   繁体   English

删除缓存的文件 - WebView Android 4.4+

[英]Delete cached files - WebView Android 4.4+

I managed to delete cached files created by WebView using: 我设法删除了由WebView创建的缓存文件:

Clearing android cache , Clear Application cache on exit in android 清除android缓存在android中退出时清除应用程序缓存

However for Android 4.4 that solution doesn't work properly, since the files are cached in: 但是对于Android 4.4,该解决方案无法正常工作,因为文件缓存在:

/data/data/com.app.package/app_webview/

instead of: 代替:

/data/data/com.app.package/cache/

The above path can be obtained by the official command getCacheDir() . 上面的路径可以通过官方命令getCacheDir()

An approach could be hard-coding the path obtained through Get Application Directory 一种方法可能是对通过Get Application Directory获得的路径进行硬编码

However, is there any [official]/proper solution to address this? 但是,有没有[官方] /适当的解决方案来解决这个问题?

you can use this code 你可以使用这段代码

    private static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (String aChildren : children) {
            boolean success = deleteDir(new File(dir, aChildren));
            if (!success) {
                return false;
            }
        }
    }
    // The directory is now empty so delete it
    return dir != null && dir.delete();

}

void trimCache() {

    try {
        String pathadmob = this.getFilesDir().getParent() + "/app_webview";
        File dir = new File(pathadmob);
        if (dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Also, here was generated all admob cache 4.4+, you can use a code to verify how many times the user use the app, and delete the admob cache when the user reached the limit. 此外,这里生成了所有admob缓存4.4+,您可以使用代码来验证用户使用该应用程序的次数,并在用户达到限制时删除admob缓存。

通常,要清除WebView缓存,请使用此WebView API: WebView.clearCache(true);

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

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