我的一个活动中有一个WebView,当它加载一个网页时,该页面会从Facebook收集一些背景数据.

但我看到的是,每次打开并刷新应用程序时,应用程序中显示的页面都是相同的.

我已经try 将WebView设置为不使用缓存,并清除WebView的缓存和历史记录.

我也遵循了这个建议:How to empty cache for WebView?

但是这些都不管用,有没有人知道我可以克服这个问题,因为它是我应用程序的重要部分.

    mWebView.setWebChromeClient(new WebChromeClient()
    {
           public void onProgressChanged(WebView view, int progress)
           {
               if(progress >= 100)
               {
                   mProgressBar.setVisibility(ProgressBar.INVISIBLE);
               }
               else
               {
                   mProgressBar.setVisibility(ProgressBar.VISIBLE);
               }
           }
    });
    mWebView.setWebViewClient(new SignInFBWebViewClient(mUIHandler));
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.clearHistory();
    mWebView.clearFormData();
    mWebView.clearCache(true);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

    Time time = new Time();
    time.setToNow();

    mWebView.loadUrl(mSocialProxy.getSignInURL()+"?time="+time.format("%Y%m%d%H%M%S"));

所以我实现了第一个建议(虽然将代码改为递归)

private void clearApplicationCache() {
    File dir = getCacheDir();

    if (dir != null && dir.isDirectory()) {
        try {
            ArrayList<File> stack = new ArrayList<File>();

            // Initialise the list
            File[] children = dir.listFiles();
            for (File child : children) {
                stack.add(child);
            }

            while (stack.size() > 0) {
                Log.v(TAG, LOG_START + "Clearing the stack - " + stack.size());
                File f = stack.get(stack.size() - 1);
                if (f.isDirectory() == true) {
                    boolean empty = f.delete();

                    if (empty == false) {
                        File[] files = f.listFiles();
                        if (files.length != 0) {
                            for (File tmp : files) {
                                stack.add(tmp);
                            }
                        }
                    } else {
                        stack.remove(stack.size() - 1);
                    }
                } else {
                    f.delete();
                    stack.remove(stack.size() - 1);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, LOG_START + "Failed to clean the cache");
        }
    }
}

然而,这仍然没有改变页面显示的内容.在我的桌面浏览器上,我在WebView中生成的网页上得到了不同的html代码,所以我知道WebView一定在某个地方缓存.

在IRC频道上,我被指向一个修复程序,可以从URL连接中移除缓存,但我还不知道如何将其应用于WebView.

http://www.androidsnippets.org/snippets/45/

如果我删除我的应用程序并重新安装,我可以将网页恢复为最新版本,即非缓存版本.主要的问题是对网页中的链接进行了更改,所以网页的前端完全没有变化.

推荐答案

Gaunt Face发布的上面编辑过的代码片段包含一个错误,即如果一个目录由于无法删除其中一个文件而无法删除,代码将在无限循环中继续重试.我将其改写为真正的递归,并添加了一个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));
}

希望对其他人有用:)

Android相关问答推荐

致命信号6(SIGABRT)MAUI应用程序在android net发布时崩溃.8、使用强制屏幕方向和并发GC

房间数据库迁移未正确处理:原因:java.lang. IllegalState异常:

编写视觉转型

Android编写动画在发布版本中崩溃

Jetpack编写Lazy列滑动删除动画不顺利结束

Jetpack Compose-如何使用值动画直接控制其他动画

Android写/读二进制文件到共享存储

Android-交叉引用表中的ForeignKey用于什么?

android crashlytics 显示崩溃但不显示我的课程中的位置

如何以编程方式通过 whatsapp android 共享图像和文本

未找到 com.android.tools.build:gradle:7.4.0 的匹配变体

无法找到方法 ''java.io.File > org.jetbrains.kotlin.gradle.tasks.KotlinCompile.getDestinationDir()

如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它

如何在jetpack compose中通过lamda返回columnScope/RowScope

java.lang.IllegalArgumentException:与请求匹配的导航目的地 NavDeepLinkRequest

将应用更改为暗模式后 Android MainActivity 数据泄漏

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

优化 Room 数据库迁移

如何在 Jetpack Compose 中更改 ModalNavigationDrawer 的抽屉容器 colored颜色 ?

Android活动系统导航栏 colored颜色 ?