prevent的Andr​​oid的WebView缓存数据 [英] Prevent Android WebView caching data

查看:125
本文介绍了prevent的Andr​​oid的WebView缓存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能prevent从缓存数据的WebView在/数据/数据​​/ ??? /缓存/ webViewCache?我已经在WebSettings设置下,但缓存文件夹仍然使用:

Is it possible to prevent a WebView from caching data in /data/data/???/cache/webViewCache? I've set the following on the WebSettings but the cache folder is still used:

webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setAppCacheEnabled(false);

我已经注意到,缓存文件将在应用程序退出或当应用程序进入后台删除,但我想preFER他们不被创建的。此外,我想prevent使用webview.db和放大器; webviewCache.db发现/数据/数据​​/ ??? /数据库。目前,我删除,像这样的数据库:

I've noticed that the cache files are deleted on application exit or when the application goes into the background but I'd prefer for them not to be created at all. Furthermore I'd like to prevent the use of the webview.db & webviewCache.db found in /data/data/???/database. I currently delete the databases like so:

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

这似乎有所需的效果和文件不会出现时可以再次使用重新创建。它是安全的假设是这种情况?

This appears to have the desired effect and the files don't appear to be recreated again for use. Is it safe to assume this is the case?

推荐答案

此页面上的音符使我相信他们不希望你有良好的访问缓存:

The notes on this page lead me to believe they don't want you to have fine access to the cache:

<一个href="http://developer.android.com/reference/android/webkit/CacheManager.html">http://developer.android.com/reference/android/webkit/CacheManager.html

据我所知,有(至少)围绕保持高速缓存两种方式。我没有写任何这在一个应用程序,所以不能保证:

As far as I can tell, there are (at least) two ways around keeping cache. I haven't written any of this in an app, so no guarantees:

(1)每次您的WebView完成一个页面,清除缓存。这样的事情在你的WebViewClient:

(1) Every time your WebView finishes a page, clear the cache. Something like this in your WebViewClient:

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    view.clearCache(true);
}

(2)如果你没有太在意什么是存储,而只是希望最新的内容,你也许可以通过设置使用loadURL(右HTTP头实现这一目标显然你要测试的这款反对你的服务器)。此外,这是仅适用于Android的API 8 +

(2) If you don't care too much what is stored, but just want the latest content, you might be able to achieve this by setting the right http headers on loadUrl (obviously you'd want to test this against your server). Also, this is only available for Android API 8+

    Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
    noCacheHeaders.put("Pragma", "no-cache");
    noCacheHeaders.put("Cache-Control", "no-cache");
    view.loadUrl(url, noCacheHeaders);

也许你想这一点,但也许还设置web视图缓存大小的东西少。我不知道如果是0将工作,所以也许1:

Maybe you tried this, but maybe also set the WebView Cache size to something small. I'm not sure if 0 will work, so maybe 1:

wv.getSettings().setAppCacheMaxSize(1);

祝您好运!

这篇关于prevent的Andr​​oid的WebView缓存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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