Saving WebView and showing it while offline?WebView load website when online, load local file when offlineSaving WebView page to cacheHow do save an Android Activity state using save instance state?Strange out of memory issue while loading an image to a Bitmap objectDownload a file with Android, and showing the progress in a ProgressDialogHow to listen for a WebView finishing loading a URL?Android App development - WebView is not workingLoad HTML file into WebViewHow to go back to previous page if back button is pressed in WebView?Android webview launches browser when calling loadurlWebView not showing video on Android 2.3.7WebView not showing some content

How to add power-LED to my small amplifier?

Basic combinations logic doubt in probability

What is the offset in a seaplane's hull?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Is the month field really deprecated?

Mains transformer blew up amplifier, incorrect description in wiring instructions?

If Manufacturer spice model and Datasheet give different values which should I use?

Why Is Death Allowed In the Matrix?

Theorems that impeded progress

The magic money tree problem

Why can't I see bouncing of a switch on an oscilloscope?

Why are weather verbs 曇る and 晴れる treated differently in this sentence?

"You are your self first supporter", a more proper way to say it

GPS Rollover on Android Smartphones

Addon: add submenu

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

How do I create uniquely male characters?

Why was the small council so happy for Tyrion to become the Master of Coin?

Can I interfere when another player is about to be attacked?

How to write a macro that is braces sensitive?

A function which translates a sentence to title-case

Can a Warlock become Neutral Good?



Saving WebView and showing it while offline?


WebView load website when online, load local file when offlineSaving WebView page to cacheHow do save an Android Activity state using save instance state?Strange out of memory issue while loading an image to a Bitmap objectDownload a file with Android, and showing the progress in a ProgressDialogHow to listen for a WebView finishing loading a URL?Android App development - WebView is not workingLoad HTML file into WebViewHow to go back to previous page if back button is pressed in WebView?Android webview launches browser when calling loadurlWebView not showing video on Android 2.3.7WebView not showing some content






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I need to cache a WebView in a way that it would use cache if there is no internet, and when there is internet it will use the online page. Since my class with the WebView does loadUrl() in the onCreateView, it reloads it every time so I need cache for 2 reasons: faster loading, but mainly for offline use of the app.



import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.net.ConnectivityManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CalendarFragment extends Fragment
private WebView webView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.calendar_layout, container, false);
webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCachePath(getContext().getCacheDir().getPath());
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://calendar.google.com/calendar/htmlembed?src=wlmacci%40gmail.com&ctz=America%2FToronto");
//webView.loadUrl("https://sites.google.com/view/wlmac/textfile?");
//webView.loadUrl("https://google.ca");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
return view;




I have tried the LOAD_DEFAULT as well as LOAD_CACHE_ELSE_NETWORK and neither seem to work; when I switch away from Calendar fragment and back to it with my WiFi off, I get net::ERR_ADDRESS_UNREACHABLE



Update: Doesn't work with LOAD_CACHE_ONLY either



Update: Added ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE, am now getting net::ERR_INTERNET_DISCONNECTED error



Sources I learned it from:
Source 1
Source 2










share|improve this question



















  • 1





    Possible duplicate of WebView load website when online, load local file when offline

    – Manoj Perumarath
    Mar 9 at 4:07











  • No I have already incorporated everything and tried all cache modes yet it still does not work

    – Andrey Starenky
    Mar 9 at 4:17






  • 1





    enable javascript and try again

    – Manoj Perumarath
    Mar 9 at 4:45











  • Added JavaScript and other booleans and still doesn't work, check updated code

    – Andrey Starenky
    Mar 9 at 5:00


















0















I need to cache a WebView in a way that it would use cache if there is no internet, and when there is internet it will use the online page. Since my class with the WebView does loadUrl() in the onCreateView, it reloads it every time so I need cache for 2 reasons: faster loading, but mainly for offline use of the app.



import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.net.ConnectivityManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CalendarFragment extends Fragment
private WebView webView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.calendar_layout, container, false);
webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCachePath(getContext().getCacheDir().getPath());
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://calendar.google.com/calendar/htmlembed?src=wlmacci%40gmail.com&ctz=America%2FToronto");
//webView.loadUrl("https://sites.google.com/view/wlmac/textfile?");
//webView.loadUrl("https://google.ca");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
return view;




I have tried the LOAD_DEFAULT as well as LOAD_CACHE_ELSE_NETWORK and neither seem to work; when I switch away from Calendar fragment and back to it with my WiFi off, I get net::ERR_ADDRESS_UNREACHABLE



Update: Doesn't work with LOAD_CACHE_ONLY either



Update: Added ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE, am now getting net::ERR_INTERNET_DISCONNECTED error



Sources I learned it from:
Source 1
Source 2










share|improve this question



















  • 1





    Possible duplicate of WebView load website when online, load local file when offline

    – Manoj Perumarath
    Mar 9 at 4:07











  • No I have already incorporated everything and tried all cache modes yet it still does not work

    – Andrey Starenky
    Mar 9 at 4:17






  • 1





    enable javascript and try again

    – Manoj Perumarath
    Mar 9 at 4:45











  • Added JavaScript and other booleans and still doesn't work, check updated code

    – Andrey Starenky
    Mar 9 at 5:00














0












0








0








I need to cache a WebView in a way that it would use cache if there is no internet, and when there is internet it will use the online page. Since my class with the WebView does loadUrl() in the onCreateView, it reloads it every time so I need cache for 2 reasons: faster loading, but mainly for offline use of the app.



import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.net.ConnectivityManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CalendarFragment extends Fragment
private WebView webView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.calendar_layout, container, false);
webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCachePath(getContext().getCacheDir().getPath());
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://calendar.google.com/calendar/htmlembed?src=wlmacci%40gmail.com&ctz=America%2FToronto");
//webView.loadUrl("https://sites.google.com/view/wlmac/textfile?");
//webView.loadUrl("https://google.ca");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
return view;




I have tried the LOAD_DEFAULT as well as LOAD_CACHE_ELSE_NETWORK and neither seem to work; when I switch away from Calendar fragment and back to it with my WiFi off, I get net::ERR_ADDRESS_UNREACHABLE



Update: Doesn't work with LOAD_CACHE_ONLY either



Update: Added ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE, am now getting net::ERR_INTERNET_DISCONNECTED error



Sources I learned it from:
Source 1
Source 2










share|improve this question
















I need to cache a WebView in a way that it would use cache if there is no internet, and when there is internet it will use the online page. Since my class with the WebView does loadUrl() in the onCreateView, it reloads it every time so I need cache for 2 reasons: faster loading, but mainly for offline use of the app.



import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.net.ConnectivityManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CalendarFragment extends Fragment
private WebView webView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.calendar_layout, container, false);
webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCachePath(getContext().getCacheDir().getPath());
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://calendar.google.com/calendar/htmlembed?src=wlmacci%40gmail.com&ctz=America%2FToronto");
//webView.loadUrl("https://sites.google.com/view/wlmac/textfile?");
//webView.loadUrl("https://google.ca");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
return view;




I have tried the LOAD_DEFAULT as well as LOAD_CACHE_ELSE_NETWORK and neither seem to work; when I switch away from Calendar fragment and back to it with my WiFi off, I get net::ERR_ADDRESS_UNREACHABLE



Update: Doesn't work with LOAD_CACHE_ONLY either



Update: Added ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE, am now getting net::ERR_INTERNET_DISCONNECTED error



Sources I learned it from:
Source 1
Source 2







java android android-webview android-websettings






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 4:52







Andrey Starenky

















asked Mar 9 at 3:25









Andrey StarenkyAndrey Starenky

35




35







  • 1





    Possible duplicate of WebView load website when online, load local file when offline

    – Manoj Perumarath
    Mar 9 at 4:07











  • No I have already incorporated everything and tried all cache modes yet it still does not work

    – Andrey Starenky
    Mar 9 at 4:17






  • 1





    enable javascript and try again

    – Manoj Perumarath
    Mar 9 at 4:45











  • Added JavaScript and other booleans and still doesn't work, check updated code

    – Andrey Starenky
    Mar 9 at 5:00













  • 1





    Possible duplicate of WebView load website when online, load local file when offline

    – Manoj Perumarath
    Mar 9 at 4:07











  • No I have already incorporated everything and tried all cache modes yet it still does not work

    – Andrey Starenky
    Mar 9 at 4:17






  • 1





    enable javascript and try again

    – Manoj Perumarath
    Mar 9 at 4:45











  • Added JavaScript and other booleans and still doesn't work, check updated code

    – Andrey Starenky
    Mar 9 at 5:00








1




1





Possible duplicate of WebView load website when online, load local file when offline

– Manoj Perumarath
Mar 9 at 4:07





Possible duplicate of WebView load website when online, load local file when offline

– Manoj Perumarath
Mar 9 at 4:07













No I have already incorporated everything and tried all cache modes yet it still does not work

– Andrey Starenky
Mar 9 at 4:17





No I have already incorporated everything and tried all cache modes yet it still does not work

– Andrey Starenky
Mar 9 at 4:17




1




1





enable javascript and try again

– Manoj Perumarath
Mar 9 at 4:45





enable javascript and try again

– Manoj Perumarath
Mar 9 at 4:45













Added JavaScript and other booleans and still doesn't work, check updated code

– Andrey Starenky
Mar 9 at 5:00






Added JavaScript and other booleans and still doesn't work, check updated code

– Andrey Starenky
Mar 9 at 5:00













1 Answer
1






active

oldest

votes


















0














Add these permissions to manifest



<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"/>


Try this code,



webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheMaxSize( 8 * 1024 * 1024 );
webView.getSettings().setAppCachePath(
getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );

if ( !isNetworkAvailable() ) //offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );


webView.loadUrl( "https://calendar.google.com/calendar/htmlembed?
src=wlmacci%40gmail.com&ctz=America%2FToronto" );


Network connectivity checking method



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





share|improve this answer

























  • Thank you, but I have tried this. I have the permissions already. getApplicationContext() doesn't work and says "Cannot resolve method getApplicationContext()" and same thing goes for the isNetworkAvailable(), it also can't resolve.

    – Andrey Starenky
    Mar 9 at 5:23











  • you need to add that method in your class, isNetworkAvailable

    – Manoj Perumarath
    Mar 9 at 5:28











  • Oh yes my bad. What about the getApplicationContext()? Also shouldn't it not make a difference for checking the network? It is already set to LOAD_CACHE_ELSE_NETWORK so it should still work. Anyways I will add the method, meanwhile what about the getApplicationContext()?

    – Andrey Starenky
    Mar 9 at 5:31











  • Can you please clarify what you mean by that? Also getSystemService() cannot be resolved

    – Andrey Starenky
    Mar 9 at 5:35











  • I did getContext() instead of getApplicationContext() do you think that's fine?

    – Andrey Starenky
    Mar 9 at 5:36











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073699%2fsaving-webview-and-showing-it-while-offline%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Add these permissions to manifest



<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"/>


Try this code,



webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheMaxSize( 8 * 1024 * 1024 );
webView.getSettings().setAppCachePath(
getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );

if ( !isNetworkAvailable() ) //offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );


webView.loadUrl( "https://calendar.google.com/calendar/htmlembed?
src=wlmacci%40gmail.com&ctz=America%2FToronto" );


Network connectivity checking method



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





share|improve this answer

























  • Thank you, but I have tried this. I have the permissions already. getApplicationContext() doesn't work and says "Cannot resolve method getApplicationContext()" and same thing goes for the isNetworkAvailable(), it also can't resolve.

    – Andrey Starenky
    Mar 9 at 5:23











  • you need to add that method in your class, isNetworkAvailable

    – Manoj Perumarath
    Mar 9 at 5:28











  • Oh yes my bad. What about the getApplicationContext()? Also shouldn't it not make a difference for checking the network? It is already set to LOAD_CACHE_ELSE_NETWORK so it should still work. Anyways I will add the method, meanwhile what about the getApplicationContext()?

    – Andrey Starenky
    Mar 9 at 5:31











  • Can you please clarify what you mean by that? Also getSystemService() cannot be resolved

    – Andrey Starenky
    Mar 9 at 5:35











  • I did getContext() instead of getApplicationContext() do you think that's fine?

    – Andrey Starenky
    Mar 9 at 5:36















0














Add these permissions to manifest



<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"/>


Try this code,



webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheMaxSize( 8 * 1024 * 1024 );
webView.getSettings().setAppCachePath(
getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );

if ( !isNetworkAvailable() ) //offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );


webView.loadUrl( "https://calendar.google.com/calendar/htmlembed?
src=wlmacci%40gmail.com&ctz=America%2FToronto" );


Network connectivity checking method



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





share|improve this answer

























  • Thank you, but I have tried this. I have the permissions already. getApplicationContext() doesn't work and says "Cannot resolve method getApplicationContext()" and same thing goes for the isNetworkAvailable(), it also can't resolve.

    – Andrey Starenky
    Mar 9 at 5:23











  • you need to add that method in your class, isNetworkAvailable

    – Manoj Perumarath
    Mar 9 at 5:28











  • Oh yes my bad. What about the getApplicationContext()? Also shouldn't it not make a difference for checking the network? It is already set to LOAD_CACHE_ELSE_NETWORK so it should still work. Anyways I will add the method, meanwhile what about the getApplicationContext()?

    – Andrey Starenky
    Mar 9 at 5:31











  • Can you please clarify what you mean by that? Also getSystemService() cannot be resolved

    – Andrey Starenky
    Mar 9 at 5:35











  • I did getContext() instead of getApplicationContext() do you think that's fine?

    – Andrey Starenky
    Mar 9 at 5:36













0












0








0







Add these permissions to manifest



<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"/>


Try this code,



webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheMaxSize( 8 * 1024 * 1024 );
webView.getSettings().setAppCachePath(
getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );

if ( !isNetworkAvailable() ) //offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );


webView.loadUrl( "https://calendar.google.com/calendar/htmlembed?
src=wlmacci%40gmail.com&ctz=America%2FToronto" );


Network connectivity checking method



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





share|improve this answer















Add these permissions to manifest



<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"/>


Try this code,



webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheMaxSize( 8 * 1024 * 1024 );
webView.getSettings().setAppCachePath(
getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );

if ( !isNetworkAvailable() ) //offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );


webView.loadUrl( "https://calendar.google.com/calendar/htmlembed?
src=wlmacci%40gmail.com&ctz=America%2FToronto" );


Network connectivity checking method



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 5:36

























answered Mar 9 at 5:16









Manoj PerumarathManoj Perumarath

1,78311229




1,78311229












  • Thank you, but I have tried this. I have the permissions already. getApplicationContext() doesn't work and says "Cannot resolve method getApplicationContext()" and same thing goes for the isNetworkAvailable(), it also can't resolve.

    – Andrey Starenky
    Mar 9 at 5:23











  • you need to add that method in your class, isNetworkAvailable

    – Manoj Perumarath
    Mar 9 at 5:28











  • Oh yes my bad. What about the getApplicationContext()? Also shouldn't it not make a difference for checking the network? It is already set to LOAD_CACHE_ELSE_NETWORK so it should still work. Anyways I will add the method, meanwhile what about the getApplicationContext()?

    – Andrey Starenky
    Mar 9 at 5:31











  • Can you please clarify what you mean by that? Also getSystemService() cannot be resolved

    – Andrey Starenky
    Mar 9 at 5:35











  • I did getContext() instead of getApplicationContext() do you think that's fine?

    – Andrey Starenky
    Mar 9 at 5:36

















  • Thank you, but I have tried this. I have the permissions already. getApplicationContext() doesn't work and says "Cannot resolve method getApplicationContext()" and same thing goes for the isNetworkAvailable(), it also can't resolve.

    – Andrey Starenky
    Mar 9 at 5:23











  • you need to add that method in your class, isNetworkAvailable

    – Manoj Perumarath
    Mar 9 at 5:28











  • Oh yes my bad. What about the getApplicationContext()? Also shouldn't it not make a difference for checking the network? It is already set to LOAD_CACHE_ELSE_NETWORK so it should still work. Anyways I will add the method, meanwhile what about the getApplicationContext()?

    – Andrey Starenky
    Mar 9 at 5:31











  • Can you please clarify what you mean by that? Also getSystemService() cannot be resolved

    – Andrey Starenky
    Mar 9 at 5:35











  • I did getContext() instead of getApplicationContext() do you think that's fine?

    – Andrey Starenky
    Mar 9 at 5:36
















Thank you, but I have tried this. I have the permissions already. getApplicationContext() doesn't work and says "Cannot resolve method getApplicationContext()" and same thing goes for the isNetworkAvailable(), it also can't resolve.

– Andrey Starenky
Mar 9 at 5:23





Thank you, but I have tried this. I have the permissions already. getApplicationContext() doesn't work and says "Cannot resolve method getApplicationContext()" and same thing goes for the isNetworkAvailable(), it also can't resolve.

– Andrey Starenky
Mar 9 at 5:23













you need to add that method in your class, isNetworkAvailable

– Manoj Perumarath
Mar 9 at 5:28





you need to add that method in your class, isNetworkAvailable

– Manoj Perumarath
Mar 9 at 5:28













Oh yes my bad. What about the getApplicationContext()? Also shouldn't it not make a difference for checking the network? It is already set to LOAD_CACHE_ELSE_NETWORK so it should still work. Anyways I will add the method, meanwhile what about the getApplicationContext()?

– Andrey Starenky
Mar 9 at 5:31





Oh yes my bad. What about the getApplicationContext()? Also shouldn't it not make a difference for checking the network? It is already set to LOAD_CACHE_ELSE_NETWORK so it should still work. Anyways I will add the method, meanwhile what about the getApplicationContext()?

– Andrey Starenky
Mar 9 at 5:31













Can you please clarify what you mean by that? Also getSystemService() cannot be resolved

– Andrey Starenky
Mar 9 at 5:35





Can you please clarify what you mean by that? Also getSystemService() cannot be resolved

– Andrey Starenky
Mar 9 at 5:35













I did getContext() instead of getApplicationContext() do you think that's fine?

– Andrey Starenky
Mar 9 at 5:36





I did getContext() instead of getApplicationContext() do you think that's fine?

– Andrey Starenky
Mar 9 at 5:36



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073699%2fsaving-webview-and-showing-it-while-offline%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived