Android的web视图 - 彻底清除缓存 [英] Android Webview - Completely Clear the Cache

查看:154
本文介绍了Android的web视图 - 彻底清除缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的WebView在我活动之一,当它加载一个网页,在网页收集的一些背景资料来自Facebook。

I have a WebView in one of my Activities, and when it loads a webpage, the page gathers some background data from Facebook.

我所看到的虽然是在应用程序中显示的页面是应用程序被打开,每次都在同一个刷新。

What I'm seeing though, is the page displayed in the application is the same on each time the app is opened and refreshed.

我已经尝试设置的WebView不使用高速缓存并清除web视图缓存和历史。

I've tried setting the WebView not to use cache and clear the cache and history of the WebView.

我也跟着在这里建议:如何清空缓存的WebView

I've also followed the suggestion here: How to empty cache for WebView?

但没有工作的,没有任何人有我什么想法可以克服这个问题,因为它是我的应用程序的重要组成部分。

But none of this works, does anyone have any ideas of I can overcome this problem because it is a vital part of my application.

    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"));

所以,我实现了第一个建议(虽然改变了code是递归)

So I implemented the first suggestion (Although changed the code to be recursive)

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");
        }
    }
}

然而,这仍然没有改变什么页面显示。在我的桌面浏览器我得到不同的HTML code在web视图产生的,所以我知道的WebView必须某处缓存网页。

However this still hasn't changed what the page is displaying. On my desktop browser I am getting different html code to the web page produced in the WebView so I know the WebView must be caching somewhere.

在IRC频道,我指着一个补丁来从URL连接删除缓存,但不能看到如何将其应用到的WebView呢。

On the IRC channel I was pointed to a fix to remove caching from a URL Connection but can't see how to apply it to a WebView yet.

<一个href="http://www.androidsnippets.org/snippets/45/">http://www.androidsnippets.org/snippets/45/

如果我删除我的应用程序,并重新安装它,我可以让网页回最新的,即非缓存的版本。的主要问题是在作出更改到在网页中的链接,所以该网页的前端是完全不变。

If I delete my application and re-install it, I can get the webpage back up to date, i.e. a non-cached version. The main problem is the changes are made to links in the webpage, so the front end of the webpage is completely unchanged.

推荐答案

上面张贴憔悴的面容的编辑code片段包含一个错误,如果一个目录无法删除,因为它的某个文件不能被删除, code将继续重试无限循环。我重写了它是真正的递归,并增加了一个numDays参数,这样你就可以控制多大的文件一定是被修剪的:

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. 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:

//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的web视图 - 彻底清除缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆