如何使webview使用HTML5缓存清单?

时间:2011-01-07 16:03:02

标签: android webview

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

3 个答案:

答案 0 :(得分:1)

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);

答案 1 :(得分:0)

试试这段代码:

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);
    }

Here链接。

答案 2 :(得分:0)

@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);
   }
 }

上述方法将用于处理离线场景中的重定向。 [为了实现appcache和路径,请参考之前的评论。

参考链接:HTML5 Cache mechanism in android