На устройствах Android до 4.4.2 браузер по умолчанию и Chrome поддерживают манифест кэша HTML5. Однако на тех же устройствах компонент WebView, похоже, не поддерживает манифест кэша HTML5. Кто-нибудь знает, как я могу заставить компонент WebView поддерживать манифест HTML5?
Как я могу заставить webview использовать манифест кэша HTML5?
Ответ 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 the DEFAULT location for your app 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);
Ответ 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 the DEFAULT location for your app 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);
}
Здесь ссылка.
Ответ 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 "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