Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. Permissions are given. I load the WebPage then switch off network (WI-FI permission is also given) and I get the error as I am trying to reload the page (now it should load from cache). Plase help!

Here is the code:

WebView engine=(WebView)findViewById(R.id.web_engine);
    engine.getSettings().setJavaScriptEnabled(true);
    engine.getSettings().setBuiltInZoomControls(true);
    engine.getSettings().setLoadsImagesAutomatically(true);
    engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    engine.setInitialScale(1);

    engine.setWebViewClient(new WebViewClient());
    
    engine.setWebChromeClient(new WebChromeClient() 
    {
          @Override
             public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,QuotaUpdater quotaUpdater)
             {
                   quotaUpdater.updateQuota(spaceNeeded * 2);
             }
          public void onProgressChanged(WebView view, int progress)
          {
              activity.setTitle("Loading...");
              activity.setProgress(progress * 100);

              if(progress == 100)
              { 
                  activity.setTitle(R.string.app_name);
              }
          }
       });
    
    String appCachePath;
    
    engine.getSettings().setDomStorageEnabled(true);
   // Set cache size to 8 mb by default. should be more than enough
    engine.getSettings().setAppCacheMaxSize(1024*1024*8);
    appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
    engine.getSettings().setAppCachePath(appCachePath);
    engine.getSettings().setAllowFileAccess(true);
    engine.getSettings().setAppCacheEnabled(true);
    engine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

     cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
     if(cm.getActiveNetworkInfo() == null || !cm.getActiveNetworkInfo().isConnected())
     {           
         appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
         engine.getSettings().setAppCachePath(appCachePath);
         
         engine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
         engine.loadUrl("http://www.google.com");
     }
     else
     {
         System.err.println("CACHE OR NETWROK");
         engine.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
         engine.loadUrl("http://www.google.com");
     }
}

And Permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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" />
like image 645
Sargius Avatar asked Nov 07 '13 18:11

Sargius


People also ask

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.

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.

Can we cache WebView in android?

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

How do I disable cache in WebView?

Just after creating your webview, before loading any pages, you can clear the cache. myBrowser. clearCache(true) - the boolean indicates if you wish to delete the cached files on disk as well.


1 Answers

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.

like image 140
anthonycr Avatar answered Sep 22 '22 01:09

anthonycr