Как сделать так, чтобы веб-просмотр использовал манифест кэша HTML5? - PullRequest
26 голосов
/ 07 января 2011

На устройствах Android до 4.4.2 включительно браузер и Chrome по умолчанию поддерживают манифест кэша HTML5. Однако на тех же устройствах компонент WebView, похоже, не поддерживает манифест кэша HTML5. Кто-нибудь знает, как я могу заставить компонент WebView поддерживать манифест HTML5?

Ответы [ 3 ]

1 голос
/ 11 сентября 2015
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);
0 голосов
/ 29 апреля 2015
@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 и path см. Предыдущий комментарий.

Ссылка: Механизм кэширования HTML5 в Android

0 голосов
/ 01 января 2015

Попробуйте этот код:

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

Здесь ссылка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...