ChatGPT解决这个技术问题 Extra ChatGPT

Android Webview - 完全清除缓存

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

不过,我看到的是,每次打开和刷新应用程序时,应用程序中显示的页面都是相同的。

我尝试将 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/

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

mWebView.getSettings().setAppCacheEnabled(false); 没用?

T
Tamil Selvan C

我找到了一个更优雅简单的清除缓存的解决方案

WebView obj;
obj.clearCache(true);

http://developer.android.com/reference/android/webkit/WebView.html#clearCache%28boolean%29

我一直在试图找出清除缓存的方法,但是我们可以从上述方法中做的就是删除本地文件,但它永远不会清除 RAM。

API clearCache 释放了 webview 使用的 RAM,因此要求重新加载网页。


最好的答案,我想知道为什么它不被接受..Kudos Akshat :)
我没有运气。想知道有什么改变吗?我可以使用 google.com 加载 WebView 并且 WebView 仍然认为我已登录,即使在 clearCache(true); 之后也是如此。
@lostintranslation 为此,您可能想要删除 cookie。虽然我相信你现在已经发现了。
需要分配对象吗? WebView obj = new WebView(this); obj.clearCache(true);不管怎样,对我来说非常好,点赞!
m
markjan

上面由 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));
}

希望对其他人有用:)


非常感谢!你们拯救了我的一天:)
很棒的例行公事,为我们节省了很多痛苦。
我可以在应用程序中使用此代码来清除手机上安装的某些应用程序的缓存吗?
如果需要删除整个目录不会 Runtime.getRuntime().exec("rm -rf "+dirName+"\n");更容易?
@source.rar 是的,但是您无法保留小于 x 天的文件,这通常是您想要的缓存文件夹。
Z
Ziem

我找到了您正在寻找的修复:

context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");

出于某种原因,Android 会错误地缓存 url,它会意外返回,而不是您需要的新数据。当然,您可以从数据库中删除条目,但在我的情况下,我只尝试访问一个 URL,因此更容易删除整个数据库。

不用担心,这些数据库只是与您的应用程序相关联,因此您不会清除整个手机的缓存。


谢谢,这是一个非常巧妙的技巧。它值得更广为人知。
这会在蜂窝中引发一个讨厌的异常:06-14 22:33:34.349: ERROR/SQLiteDatabase(20382): 无法打开数据库。关闭它。 06-14 22:33:34.349: ERROR/SQLiteDatabase(20382): android.database.sqlite.SQLiteDiskIOException: 磁盘 I/O 错误 06-14 22:33:34.349: ERROR/SQLiteDatabase(20382): at android.database。 sqlite.SQLiteDatabase.native_setLocale(本机方法)
干杯拉斐尔,我想这是因为原始问题已在 Honeycomb 中解决。有谁知道是否是这种情况?
只需在 onBackpress() 或后退按钮中放入 2 行,就不会在后退堆栈中保留任何历史记录,这节省了很多时间。
K
Kingston

要在您从 APP 注销时清除所有 webview 缓存:

CookieSyncManager.createInstance(this);         
CookieManager cookieManager = CookieManager.getInstance();        
cookieManager.removeAllCookie();

棒棒糖及以上:

CookieSyncManager.createInstance(this);         
CookieManager cookieManager = CookieManager.getInstance();        
cookieManager.removeAllCookies(ValueCallback);

拯救我的生命和一天。
如果您在活动中无权访问 webview,则可以正常工作。另请注意,此 API 已被弃用,因此请在 L+ 设备上使用“removeAllCookies(ValueCallback)”API。
我应该用 ValueCallBack 替换什么?
@QaisarKhanBangash new ValueCallback() { atOverride public void onReceiveValue(Boolean value) { } }
S
Srinivasan

要从 Webview 清除 cookie 和缓存,

    // Clear all the Application Cache, Web SQL Database and the HTML5 Web Storage
    WebStorage.getInstance().deleteAllData();

    // Clear all the cookies
    CookieManager.getInstance().removeAllCookies(null);
    CookieManager.getInstance().flush();

    webView.clearCache(true);
    webView.clearFormData();
    webView.clearHistory();
    webView.clearSslPreferences();

K
Ketan Ramani

唯一适合我的解决方案

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    CookieManager.getInstance().removeAllCookies(null);
    CookieManager.getInstance().flush();
} 

Z
Ziem

这应该清除您的应用程序缓存,这应该是您的 webview 缓存所在的位置

File dir = getActivity().getCacheDir();

if (dir != null && dir.isDirectory()) {
    try {
        File[] children = dir.listFiles();
        if (children.length > 0) {
            for (int i = 0; i < children.length; i++) {
                File[] temp = children[i].listFiles();
                for (int x = 0; x < temp.length; x++) {
                    temp[x].delete();
                }
            }
        }
    } catch (Exception e) {
        Log.e("Cache", "failed cache clean");
    }
}

试过这个(稍微改变了代码),仍然得到相同的结果 - >上面解释过
k
kamal khalaf
webView.clearCache(true)
appFormWebView.clearFormData()
appFormWebView.clearHistory()
appFormWebView.clearSslPreferences()
CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush()
WebStorage.getInstance().deleteAllData()

E
Ercan

只需在 Kotlin 中使用以下代码即可为我工作

WebView(applicationContext).clearCache(true)

W
WaqasArshad
CookieSyncManager.createInstance(this);         
CookieManager cookieManager = CookieManager.getInstance();        
cookieManager.removeAllCookie();

D
Dũng Phạm Tiến
CookieSyncManager.createInstance(this);    
CookieManager cookieManager = CookieManager.getInstance(); 
cookieManager.removeAllCookie();

它可以在我的 webview 中清除 google 帐户


CookieSyncManager 已被贬低
A
Aduait Pokhriyal

要清除历史记录,只需执行以下操作:

this.appView.clearHistory();

来源:http://developer.android.com/reference/android/webkit/WebView.html


A
Aduait Pokhriyal

确保您使用以下方法,表单数据在单击输入字段时不会显示为自动弹出。

getSettings().setSaveFormData(false);

J
Joundill
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db")

做到了


I
Iskandir

要完全清除 kotlin 中的缓存,您可以使用:

context.cacheDir.deleteRecursively()

以防万一有人需要 kotlin 代码(: