我有一个包含以下配置的Web视图:
getSettings().setJavaScriptEnabled(true); getSettings().setSupportMultipleWindows(false); getSettings().setNeedInitialFocus(false); getSettings().setSupportZoom(false); getSettings().setAllowFileAccess(true); getSettings().setAppCacheEnabled(true); getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); String cachePath = getContext().getCacheDir().getAbsolutePath(); getSettings().setAppCachePath(cachePath);
我加载一个html页面,然后加载一些资产:一些静态图像,几个js文件,一个CSS和一个字体.
使用缓存控制标头返回这些资产:
HTTP/1.1 200 OK Date: Wed, 01 Jan 2014 18:40:36 GMT Last-Modified: Mon, 30 Dec 2013 09:23:55 GMT Cache-Control: public, max-age=604800
我重新加载了url,我可以在fiddler中看到所有资产都在本地缓存,没有重新加载.
但是,如果我关闭应用程序并再次打开它 - 一切都会再次下载.
如何确保资产(js\css\images)缓存在设备上,而不是每次都从我的服务器中检索?
事实证明,此代码效果很好。
问题是我正在调用clearCache
另一个Webview,结果是:
缓存是针对每个应用程序的,因此将清除所有使用的WebView的缓存。
当我clearCache
从另一个Web视图中删除对的调用后,一切都运行良好。