4

I'm trying to show some webpages via webview without connecting to the internet. I thought I can save the webpages in the cache and load them from the cache again if there is no connection to the internet. But it's not working. The website don't appear, instead I get that the website is not available right now. I already checked if the AppCachePath is correct with getCacheDir(). Do you have any idea, what I'm doing wrong, or how it works. Would be perfect. Thanks a lot.

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
webView.getSettings().setAppCachePath("/data/data/de.app/cache");
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,
                                         WebStorage.QuotaUpdater quotaUpdater)
    {
        quotaUpdater.updateQuota(spaceNeeded * 2);
    }
});

ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected() == true)
{
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webView.loadUrl("http://www.google.de");
}
else{
    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
    webView.loadUrl("http://www.google.de");
}
Anne
  • 26,765
  • 9
  • 65
  • 71
user1029875
  • 53
  • 1
  • 5

1 Answers1

3

you can try with webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

OWADVL
  • 10,704
  • 7
  • 55
  • 67