Android WebView isn't loading cached Website if there is no connection

后端 未结 1 1191
一整个雨季
一整个雨季 2021-02-05 21:47

I am trying to cache the website loaded in WebView but I can\'t get it working if the network connection is OFF.

Cachdirectory is created, cached files are there. Permi

1条回答
  •  死守一世寂寞
    2021-02-05 22:17

    Your use of

    engine.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    

    is telling the WebView to load the page from cache, but if it needs anything that isn't in the cache, it looks to the network, and when you have no connection, it will just give you the "page could not be loaded error." This is because sadly not everything is stored in the cache and even with this setting, you will notice the browser using the network.

    The only way to get the WebView to ignore no connection is to use the

    engine.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
    

    setting instead. Some images will not load with this setting (because they weren't cached) but it will still load everything it finds in the cache.


    One other note unrelated to your question is that in your code, you have setAppCacheMaxSize being used and that has been deprecated in API 18 and above because the WebView manages the cache size itself, so just a heads up about that.

    0 讨论(0)
提交回复
热议问题