我的一个活动中有一个 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);
没用?
我找到了一个更优雅简单的清除缓存的解决方案
WebView obj;
obj.clearCache(true);
http://developer.android.com/reference/android/webkit/WebView.html#clearCache%28boolean%29
我一直在试图找出清除缓存的方法,但是我们可以从上述方法中做的就是删除本地文件,但它永远不会清除 RAM。
API clearCache 释放了 webview 使用的 RAM,因此要求重新加载网页。
上面由 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));
}
希望对其他人有用:)
我找到了您正在寻找的修复:
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
出于某种原因,Android 会错误地缓存 url,它会意外返回,而不是您需要的新数据。当然,您可以从数据库中删除条目,但在我的情况下,我只尝试访问一个 URL,因此更容易删除整个数据库。
不用担心,这些数据库只是与您的应用程序相关联,因此您不会清除整个手机的缓存。
要在您从 APP 注销时清除所有 webview 缓存:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
棒棒糖及以上:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(ValueCallback);
要从 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();
唯一适合我的解决方案
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
}
这应该清除您的应用程序缓存,这应该是您的 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");
}
}
webView.clearCache(true)
appFormWebView.clearFormData()
appFormWebView.clearHistory()
appFormWebView.clearSslPreferences()
CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush()
WebStorage.getInstance().deleteAllData()
只需在 Kotlin 中使用以下代码即可为我工作
WebView(applicationContext).clearCache(true)
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
它可以在我的 webview 中清除 google 帐户
要清除历史记录,只需执行以下操作:
this.appView.clearHistory();
来源:http://developer.android.com/reference/android/webkit/WebView.html
确保您使用以下方法,表单数据在单击输入字段时不会显示为自动弹出。
getSettings().setSaveFormData(false);
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db")
做到了
要完全清除 kotlin 中的缓存,您可以使用:
context.cacheDir.deleteRecursively()
以防万一有人需要 kotlin 代码(: