我有一個WebView控件,我想每次顯示當前網站時,但我認爲WebView只顯示網站的緩存版本。我嘗試了一切發現here但似乎沒有工作。我也嘗試從context.getCacheDir()中刪除所有文件,但沒有成功。我能做什麼?Android WebView - 清除緩存
這是onCreate方法我的代碼:
clearCache(this, 0);
setContentView(R.layout.webview);
deleteDatabase("webview.db");
deleteDatabase("webviewCache.db");
mWebView = (WebView)findViewById(R.id.webview);
mWebView.setWebViewClient(new CenterWebViewClient(this));
mWebView.setWebChromeClient(new CenterWebChromeClient(this));
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setAppCacheMaxSize(1);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
String url = "https://www.exacmple.com";
if (url != null) {
Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
noCacheHeaders.put("Pragma", "no-cache");
noCacheHeaders.put("Cache-Control", "no-cache");
mWebView.loadUrl(url, noCacheHeaders);
}
clearCache功能:
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("WebView", String.format("Failed to clean the cache, error %s", e.getMessage()));
}
}
return deletedFiles;
}
public static void clearCache(final Context context, final int numDays) {
Log.i("WebView", String.format("Starting cache prune, deleting files older than %d days", numDays));
int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);
Log.i("WebView", String.format("Cache pruning completed, %d files deleted", numDeletedFiles));
}
http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache。 http://stackoverflow.com/questions/6542943/clearing-all-cached-items-created-by-a-webview – Raghunandan
我做到了,不起作用 –
顯示你做了什麼 – Raghunandan