2011-01-07 56 views
25

在Android设备上达到并包括4.4.2,默认浏览器和Chrome支持HTML5缓存清单。但是,在这些相同的设备上,WebView组件似乎不支持HTML5缓存清单。有人知道我怎样才能让WebView组件支持HTML5清单吗?如何让webview使用HTML5缓存清单?

+4

我发现结果在这里:http://alex.tapmania.org/?p=110 – 2011-01-09 19:47:42

+0

可能是最不直观的方面我好像从发布解决方案您必须手动设置WebView的appCachePath,否则它将无法工作。你不会知道为什么它不起作用。它不会。 – KevinH 2012-02-27 16:43:07

回答

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的链接。

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

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