【问题标题】:How can I make webview use the HTML5 cache manifest?如何让 webview 使用 HTML5 缓存清单?
【发布时间】:2011-01-07 16:03:01
【问题描述】:

在 4.4.2 及之前的 Android 设备上,默认浏览器和 Chrome 支持 HTML5 缓存清单。但是,在这些相同的设备上,WebView 组件似乎不支持 HTML5 缓存清单。有人知道如何使 WebView 组件支持 HTML5 清单吗?

【问题讨论】:

  • 我在这里找到了结果:alex.tapmania.org/?p=110
  • 可能我从发布的解决方案中获得的最不直观的方面是您必须手动设置 WebView 的 appCachePath,否则它将不起作用。你不会知道为什么它不起作用。它只是不会。
  • 我真的不明白这太本地化了
  • 我刚遇到这个问题。有一个批准的答案会很好 - 我不同意关闭。
  • 如果我没记错的话(因为这是前段时间),您还应该确保您设置的文件路径也存在。当文件夹不存在时,某些设备似乎可以工作,而某些设备确实可以工作(这也可能是由不同版本的 Android 引起的)。只是一个想法。

标签: android webview


【解决方案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);

【讨论】:

  • webView.getSettings().setAppCacheMaxSize(1024*1024*8) 被贬低
【解决方案2】:

试试这个代码:

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

【讨论】:

    【解决方案3】:
    @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

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 2010-09-21
      • 2011-01-04
      • 2016-07-04
      • 2011-11-27
      相关资源
      最近更新 更多