My Android Studio App has to show an offline site if there's no internet connection. And show an online website if internet connection is active.
Now I look up for the function if a Internet connection is active the online site is showing now. If i deactivate now the Internet connection it has to show the offline site again and not the standard "Error Site is not avaiable" - Error Page. How to fix this?
WebView wb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wb = (WebView) findViewById(R.id.mywb);
wb.setWebViewClient(new MyBrowser());
if ( !isNetworkAvailable() ) { // loading offline
wb.loadUrl("file:///android_asset/index.html");
}else { // loading online
wb.loadUrl("http://www.google.com");
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}