0

I am using WebView to view site content. I use the following code to show an error page if there is no internet connection:

    mywebview.setWebViewClient(new WebViewClient(){
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            mywebview.loadUrl("file:///android_asset/error.html");

        }
    });
}

and it is working fine if it is the first time to open the app but if you open it again without internet connection the error page doesn't appear but it uses the cached version and show the site .. and you can open any page you opened before - at the first time - but the other pages just freeze and take no action when you try to open.

what I need is to monitor the internet connection every time you open the app and also while the app is running and show a message tell the user that there is no connection.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • If you want to clear cache so next time when load webview it will not support cached page use "mywebview.clearCache(true);". Why you are calling "setWebViewClient(new WebViewClient())" two times? – Abhishek Mar 29 '18 at 03:40
  • @Abhishek thanks .. your clear cache code works fine and solve the first part of the problem .. now the error page appear every time app work without internet .. but i still want to show a message if the user lost connection while using the app because the site content is freezing when that happen.. and for the twice calling it was a mistake and i edited the code .. – Mahmoud Manson Mar 29 '18 at 04:01
  • You are saying need to detect no internet connection? From internet getting information you are calling some api's or url's. To call such api's or to load url's what kind of code you are using any pure android code or javascript or jquery? So I can try for same. – Abhishek Mar 29 '18 at 04:42
  • @Abhishek it is javascript ... – Mahmoud Manson Mar 29 '18 at 05:29

2 Answers2

0

I hope my answer will help you. To monitor internet connection , Use this method and call this method whenever you want to check internet connection. If this method return false, internet connection is not active and call alert dialog box to show user that there is no internet connection.

   public boolean networkcheck() {

   ConnectivityManager connectivityManager = 
   (ConnectivityManager)  getSystemService(CONNECTIVITY_SERVICE);
   NetworkInfo activeNetworkInfo =  onnectivityManager.getActiveNetworkInfo();   

   return activeNetworkInfo != null && activeNetworkInfo.isConnected();

}
0

To clear cache need to call

mywebview.clearCache(true); //Which avoid loading page from cache

No internet connection detect

As you are using javascript you have to first think about some cases

  1. No internet connection to device
  2. If request from device to your server is failed (This is tricky)

For above first point i.e. No internet connection to device this link helps you.

For above second point i.e If request from device to your server is failed you need to check your request timeout and other possibilities. You will get more discussion here

Abhishek
  • 3,348
  • 3
  • 15
  • 34
  • the js code works with me on PC but it doesn't on android device .. the site content still freezing and couldn't catch the js code .. i tried that while using and not using the webview clear cache code – Mahmoud Manson Mar 29 '18 at 16:17
  • Let me check. I will try and send. – Abhishek Apr 02 '18 at 03:11