android–WebView:以编程方式清除服务工作者缓存

android–WebView:以编程方式清除服务工作者缓存,第1张

概述为了让用户快速清除缓存,我使用了附加到ClearCache按钮的以下功能(基于this和this):staticvoidclearAppCache(Contextcontext){try{Filedir=context.getCacheDir();deleteDir(dir);}catch(Exceptione){//TODO:handleexcepti

为了让用户快速清除缓存,我使用了附加到Clear Cache按钮的以下功能(基于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现在正在加载一个使用服务工作者的页面,我发现这似乎并没有清除服务工作者缓存.我有一个用户的报告,为了真正清除服务工作者的东西,他们必须手动清除以下文件夹的内容(在他们的root设备上):

/data/data/com.example.myapp/app_webvIEw/Cache/

基于this post,我尝试将以下行添加到clearappCache()函数中:

WebStorage.getInstance().deleteallData();

但是,这似乎没有清除服务工作者缓存的效果.

有任何想法吗?是的我知道可以使用JavaScript清除服务工作者缓存(请参阅上面链接的帖子),但我想要一种直接从AndroID执行此 *** 作的方法.

解决方法:

我现在找到了一种删除服务工作缓存的方法.我的数据目录位于:

/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

请注意app_webvIEw中存在Service Worker子目录,这是一个赠品.

因此,要清除服务工作者缓存,您似乎只需要删除该子目录:

file dataDir = context.getDataDir(); // or see https://stackoverflow.com/a/19630415/4070848 for older AndroID versionsfile 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 versionsfile 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中存在Cache子目录,并且它们位于不支持服务工作者的KitKat(AndroID 4.4)上…不确定为什么app_webvIEw / Cache目录正在使用中而不是(或除了)缓存.我根本没有app_webvIEw / Cache.

总结

以上是内存溢出为你收集整理的android – WebView:以编程方式清除服务工作者缓存全部内容,希望文章能够帮你解决android – WebView:以编程方式清除服务工作者缓存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1108826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存