开发者

How can I make webview use the HTML5 cache manifest?

On Android devices up to and including 4.4.2, the default Browser and Chrome support the HTML5 cache manifest. However, on those same devices, the WebView component does not seem to support the HTML5 cache manifest. Does anybody know how I can make the WebView com开发者_JAVA百科ponent support the HTML5 manifest?


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


Try this code:

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 the link.


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

Above method will be used to handle redirection in offline scenario. [For implementing appcache and path refer previous comment.

Reference Link : HTML5 Cache mechanism in android

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜