78

I am actually new to programming in Java but I have been following several solutions to my problem here but didn't find one that suits my case and I can't seem to get the code down correctly.

I would like to have a WebView that opens an online page (for example Google) when the phone is online and open a local HTML page when the phone is offline.

At the same time though I want the phone to overwrite the local page when it is online so that the offline local page is always updated to the last time the phone was connected to the internet.

Any ideas on how this could be done? Some simple pointing to the right direction could help.

Thanks a lot.

SpaceCore186
  • 586
  • 1
  • 8
  • 22
mstation
  • 1,077
  • 2
  • 9
  • 15

2 Answers2

134

That sounds like a simple webview caching mechanism to me.

The following should do what you are looking for:

WebView webView = new WebView( context );
webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default

if ( !isNetworkAvailable() ) { // loading offline
    webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}

webView.loadUrl( "http://www.google.com" );

The method isNetworkAvailable() checks for an active network connection:

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

Finally, don't forget to add the following three permissions to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
jenzz
  • 7,239
  • 6
  • 49
  • 69
  • 2
    this is really complete.. works like a charm! thank you a lot for showing me the cache mode – mstation Feb 03 '13 at 10:22
  • 1
    @jenzz: I have two problems with your solution. First, in my case, all that appears in the cache dir (external sd card) is an *empty* file called `ApplicationCache.db`. Shouldn't be bigger?? Maybe the cache is being stored elsewhere? Second, the `.setAppCacheMaxSize()` thing is deprecated in API18, not sure how to add more chache then... – Luis A. Florit Mar 24 '14 at 20:10
  • In future quota will be managed automatically. There is no need to setting them. – anshad Apr 24 '14 at 12:55
  • 5
    "That sounds like a simple webview caching mechanism to me." thug life 8) – Andressa Pinheiro Dec 10 '15 at 20:27
  • 3
    Does not work for me when the app is killed and loaded again in offline mode. Good work still +1 – Skynet Mar 22 '16 at 05:50
  • Now, even when the internet is available, it loads the page from cache! @jenzz kindly help me with it! – Akash Dubey Aug 10 '16 at 06:13
  • It works only for HTTP GET Method. However form submission still raise [offline Page Error]. Any idea? – Khalil Laleh May 04 '17 at 11:34
  • Actually I am using google drive link for my URL , and it is not working.Google drive doesn't allow to store cache? – Akshay Raut Jun 07 '20 at 05:25
  • @jenzz webView.getSettings().setAppCacheEnabled( true ); deprecated – Girish Mar 01 '21 at 11:10
  • Images of the web page are not cached by using this. Any solution for this? – Rajat Mehra Aug 18 '22 at 09:33
  • Year 2023, some methods depraceted... Moreover, this solution not work when internet oflline atm... – Ucdemir Jan 03 '23 at 16:51
27

here are occasions that a WebView cannot be cached natively. If the page header contains the following fields, the WebView will not be able to cache the contents from that page. Cache-Control: no-store, no-cache Pragma: no-cache

In this case, youwill have to modify the page property on the server to solve the caching problem.

Jeppe Leth
  • 270
  • 4
  • 4