为了为用户清除缓存提供一种快捷的方法,我使用了以下函数(基于this和this)附加到一个清除缓存按钮上:
static void clearAppCache(Context context) {
    try {
        File dir = context.getCacheDir();
        deleteDir(dir);
    } catch (Exception e) {
        // TODO: handle exception
    }
}
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;
            }
        }
        return dir.delete();
    } else if (dir!= null && dir.isFile()) {
        return dir.delete();
    } else {
        return false;
    }
}我还使用相同的缓存路径设置我的WebView,如下所示:
WebSettings webSettings = mWebView.getSettings();
webSettings.setAppCacheEnabled(true);
String cachePath = getApplicationContext().getCacheDir().getAbsolutePath();
webSettings.setAppCachePath(cachePath);我的理论是,调用clearAppCache()也会清除WebView的缓存,因为它所做的就是清除我为WebView设置的相同的缓存文件夹。
但是,由于我的WebView现在正在加载一个使用服务辅助器的页面,我发现这似乎没有清除服务工作者缓存。有一位用户报告说,要真正清除服务工作者的内容,他们必须手动清除以下文件夹的内容(在他们的根设备上):
/data/data/com.example.myapp/app_webview/Cache/基于this post,我尝试在我的clearAppCache()函数中添加以下行:
WebStorage.getInstance().deleteAllData();但是,这似乎并没有清除服务工作者缓存的效果。
有什么想法吗?是的,我知道可以使用javascript清除服务工作者缓存(请参阅上面链接的post),但是我想要一种直接从Android中实现这一点的方法。
发布于 2017-11-23 08:35:17
我现在找到了一种删除服务工作者缓存的方法。我的数据目录位于:
/data/user/0/com.app.package然后在这个范围内:
/cache
    /http
    /org.chromium.android_webview
    /WebView
/code_cache
/files
/shared_prefs
/app_webview
    /webview_data.lock
    /Web Data
    /Web Data-journal
    /metrics_guid
    /Cookies
    /Cookies-journal
    /GPUCache
    /Service Worker
    /QuotaManager
    /QuotaManager-journal
    /databases
/app_textures
/app_download_internal
/databases
/shaders请注意Service Worker子目录在app_webview中的存在,这是一种赠与。
因此,要清除服务工作者缓存,似乎只需删除该子目录:
File dataDir = context.getDataDir(); // or see https://stackoverflow.com/a/19630415/4070848 for older Android versions
File serviceWorkerDir = new File(dataDir.getPath() + "/app_webview/Service Worker/");
deleteDir(serviceWorkerDir); // function defined in original post或者,更残忍的是,您似乎可以删除整个app_webview子文件夹及其中的所有内容:
File dataDir = context.getDataDir(); // or see https://stackoverflow.com/a/19630415/4070848 for older Android versions
File appWebViewDir = new File(dataDir.getPath() + "/app_webview/");
deleteDir(appWebViewDir); // function defined in original post仍然让我困惑的是,尽管将WebView和webSettings.setAppCachePath(cachePath)的缓存路径设置在缓存目录中(请参阅我最初的文章),WebView还是选择了使用app_webview进行服务工作者缓存。也许它使用缓存目录来进行传统的http缓存,并选择自己的位置(app_webview)作为服务工作人员缓存?不过,这似乎还是不对。此外,如前所述,有一个用户报告app_webview中存在一个app_webview子目录,他们在KitKat (Android4.4)上,不支持服务工作者.不确定为什么要使用这个app_webview/Cache目录而不是(或者除了) cache。我的身上根本没有app_webview/Cache。
发布于 2017-11-21 15:40:29
我不熟悉Android<->WebView的交互。但是,假设您可以从本地原点的页面上下文中运行JavaScript,下面的代码应该清除您的源缓存存储API中的所有内容:
async function clearAllCaches() {
  const allCaches = await caches.keys();
  for (const cache of allCaches) {
    caches.delete(cache);
  }
}使用 header是另一种选择,如果您能够控制为您的WebView提供服务的远程61+服务器,并且您知道您的用户将拥有基于Chrome 61+的WebView。
https://stackoverflow.com/questions/47414581
复制相似问题