我如何使webview使用HTML5caching清单?

在直到4.4.2(包括4.4.2)的Android设备上,默认的浏览器和Chrome支持HTML5caching清单。 但是,在这些相同的设备上,WebView组件似乎不支持HTML5caching清单。 有谁知道我可以如何使WebView组件支持HTML5清单?

webView.getSettings().setDomStorageEnabled(true); // Set cache size to 8 mb by default. should be more than enough webView.getSettings().setAppCacheMaxSize(1024*1024*8); // This next one is crazy. It's the DEFAULT location for your app's cache // But it didn't work for me without this line webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache"); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 

试试这个代码:

 private void enableHTML5AppCache() { webView.getSettings().setDomStorageEnabled(true); // Set cache size to 8 mb by default. should be more than enough webView.getSettings().setAppCacheMaxSize(1024*1024*8); // This next one is crazy. It's the DEFAULT location for your app's cache // But it didn't work for me without this line webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache"); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); } 

这里的链接。

 @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // The magic redirect if( "http://HTML5app.com/app/".equals(failingUrl) ) { // main.html is the place we are redirected to by the server if we are online mWebView.loadUrl("http://HTML5app.com/app/main.html"); return; } else if( "http://HTML5app.com/app/main.html".equals(failingUrl) ) { // The cache failed - We don't have an offline version to show // This code removes the ugly android's "can't open page" // and simply shows a dialog stating we have no network view.loadData("", "text/html", "UTF-8"); showDialog(DIALOG_NONETWORK); } } 

以上方法将用于处理离线情况下的redirect。 [为了实现appcache和path,请参阅之前的评论。

参考链接: Android中的HTML5caching机制