【发布时间】:2012-10-28 14:14:21
【问题描述】:
我正在构建一个原生应用程序,其中(至少)一部分是 Web 应用程序,伪装成原生应用程序。 Facebook 向我们展示了使用良好缓存的重要性,而我正在缓存网页,所以当您第二次加载它时,它感觉就像是应用程序的原生部分。但是,按照我的设置方式,缓存的内容是永久性的。因此,如果我更改服务器上的某些内容,旧的缓存内容仍然会显示。
这是我希望达到的目标,按优先顺序排列,1 是最可取的。
- 缓存的内容首先显示。在后台,页面会检查更新的版本,但只有在内容不同时才会重新加载。
- OR 缓存的内容首先显示。页面在后台刷新并重新加载。
- 或者缓存的内容在 X 次(一周?)后被清除,迫使页面重新加载。
最后一个选项是最不可取的,因为每次重新加载页面时,都会感觉缓慢而可怕。第二个选项是我的本机活动的行为方式(首先显示缓存,在后台刷新。但是,如果用户同时向下滚动,这会将用户跳转到列表的顶部。)。第一个绝对完美!
这是我目前正在使用的代码。
WebView wv = (WebView) v.findViewById(ref_id);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setDatabaseEnabled(true);
String appDBPath = v.getContext().getApplicationContext().getDatabasePath("AppConstructor").getAbsolutePath();
wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onReachedMaxAppCacheSize(long spaceNeeded,
long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(spaceNeeded * 2);
}
@Override
public void onExceededDatabaseQuota(String url,
String databaseIdentifier, long currentQuota,
long estimatedSize, long totalUsedQuota,
WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(estimatedSize * 2);
}
});
wv.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
String appCachePath = v.getContext().getApplicationContext().getCacheDir().getAbsolutePath();
wv.getSettings().setAppCachePath(appCachePath);
wv.getSettings().setAllowFileAccess(true);
wv.getSettings().setAppCacheEnabled(true);
wv.loadUrl(ref_arg1);
Core x = new Core();
wv.addJavascriptInterface(x.new JavaScriptInterface(v.getContext()),"Android");
任何帮助将不胜感激!
【问题讨论】:
标签: java android caching webview android-webview