Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying cached version of webview in android

I am trying to get the HTML5 offline cached version of a website to display when the network is down inside of a webview.

I have overridden onReceivedError ok, and when the network is down this method is called. Problem is that it displays the generic "Web Page is not available" message.

How can I get it to display the HTML5 cached version of the page? The offline storage of the webapp is definately working, as it works fine in the desktop version of Firefox and Chrome.

I know I can call loadData into the view manually in onReceivedError, but im not sure where I can get the HTML5 cached value from.

Note: If I set some dummy data in loadData such as view.loadData(Uri.encode("<html><div>Page load failed</div></html>"), "text/html", "UTF-8"); and then click back (by detecting back event and calling webview.goBack(); then the cached version of the page is displayed ok.

Here are some lines of code I added to setup the webview:

webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webview.getSettings().setAppCacheMaxSize(1024*1024*8);                         
webview.getSettings().setAppCachePath("/data/data/com.stuff.android/cache");
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);
like image 445
Marty Avatar asked Dec 11 '11 22:12

Marty


People also ask

Can we cache WebView in android?

You can use the WebView cache to enable caching in WebView.

Does WebView have cache?

WebViews are android views, that when created, consume context . And if we cache activity context on the application level, we might end up leaking the activity that was used to create the web-view.

How do I enable cache in WebView?

This example demonstrate about How to enable app cache for webview in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Can I clear cache Android system WebView?

Open your browser. Android browser: Go to Menu > More > Settings or Menu > Settings > Privacy & Security. Chrome: Go to Menu > Settings > Privacy. Android browser: Tap Clear cache, Clear history, and Clear all cookie data as appropriate.


3 Answers

Try to find out the network status using

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

(Detect whether there is an Internet connection available on Android)

This also needs

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your AndroidManifest.xml

Now you can set the cache behaviour either to LOAD_CACHE_ONLY or to LOAD_NO_CACHE, depending on whether a network connection is available, with

webview.getSettings().setCacheMode(...)
like image 122
hlt Avatar answered Oct 22 '22 13:10

hlt


I think a good solution would be to use LOAD_NORMAL and onReceivedError Navigate BACK. I think this will load the cache according to the documentation (not sure if I remember correctly) but be carefull no to get stuck in an infinite loop


It is weird.. According to the documentation:

Override the way the cache is used. The way the cache is used is based on the navigation option. For a normal page load, the cache is checked and content is re-validated as needed. When navigating back, content is not revalidated, instead the content is just pulled from the cache. This function allows the client to override this behavior.

However the behavior you want does not seem to be one of these:

  1. LOAD_CACHE_ELSE_NETWORK

    Use cache if content is there, even if expired (eg, history nav) If it is not in the cache, load from network.

  2. LOAD_CACHE_ONLY

    Don't use the network, load from cache only.

  3. LOAD_DEFAULT

    Default cache usage pattern

  4. LOAD_NORMAL

    Normal cache usage pattern

  5. LOAD_NO_CACHE

    Don't use the cache, load from network


I do not know if you can subclass the WebView to get the desired flow.


like image 40
Sherif elKhatib Avatar answered Oct 22 '22 12:10

Sherif elKhatib


Does it not work if you simply let the browser handle this? Specify the manifest in the HTML tag like this:

<html manifest="mycache.appcache">

...and the browser should automatically use it when there's no connection available, you shouldn't need to change any settings at all.

like image 38
AshleysBrain Avatar answered Oct 22 '22 14:10

AshleysBrain