Reputation: 2557
I have a webpage with service worker, that in offline mode, mobile Chrome can still show my offline.html.
Now I have an Android APP with a webview to show the page, but when there is no internet, there is a Android error page saying "Webpage not available
The webpage at xxxxxx could not be loaded because: net:ERR_FAILED"
How can I solve it? Can I prevent checking of internet connection in the Andoird APP? Thank you very much!
Upvotes: 1
Views: 1619
Reputation: 2348
You need to override your own WebViewClient
and check the Internet connection in in shouldOverrideUrlLoading
public boolean checkInternetConnection(Context context) {
ConnectivityManager con_manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
return (con_manager.getActiveNetworkInfo() != null
&& con_manager.getActiveNetworkInfo().isAvailable()
&& con_manager.getActiveNetworkInfo().isConnected());
}
class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!checkInternetConnection(this)) {
view.loadUrl("file:///android_asset/filename.html");
} else {
view.loadUrl(url);
}
return true;
}
}
And ofcourse, set your custom WebViewClient
to your WebView
webview.setWebViewClient(new CustomWebViewClient())
Also, put filename.html
in your Android project assets folder
Upvotes: 1