Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview not loading offline cached data

I am having a problem with Webview in that it will not use the cache. I start my app up, load the HTML5 page, then back out of the page, enter airplane mode on the phone, then try to go to the web page again. It should be cached, but I get a message saying that the URL could not be retrieved.

Here is my code pertaining to this. Am I doing something wrong???

String weblink = "http://abcd.com";
final ConnectivityManager conMgr =  (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);

final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();

if (activeNetwork != null && activeNetwork.isConnected()) {
    progressBar = ProgressDialog.show(this, "Please Wait", "loading online..");
    mWebview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    mWebview.getSettings().setAppCacheMaxSize(1024*1024*8);
    mWebview.loadUrl(weblink);
    setContentView(mWebview);
} 
else 
{
    progressBar = ProgressDialog.show(this, "Please Wait", "loading offline..");
    mWebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);

    mWebview.loadUrl(weblink);
    setContentView(mWebview);
}
like image 394
sudhakar singh Avatar asked Nov 14 '22 02:11

sudhakar singh


1 Answers

Your code is ok.

Either the Website does not exist or didn't get cached correctly, or your forgot to add the following permissions :

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permisson.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
like image 82
Zakaria Avatar answered Dec 31 '22 21:12

Zakaria