title: Assign alert message while no internet in android app
body: <p>I need to show alert message while no internet connection.I have added the following code .But the alert box is not showing while no internet connection.</p>
  
  <p>Here is the code:</p>
  
  <pre><code> if (isNetworkAvailable(getApplicationContext()))
   {
         // Do whatever you want to do
   }
   else{
  
      try {
  
          AlertDialog.Builder builder = new AlertDialog.Builder(LiveChat.this);
  
           builder.setTitle("Message");
          builder.setMessage("Do you want to end the chat session?");
          // Add the buttons
  
          builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
  
                          dialog.cancel();
                      }
  
          });
          AlertDialog alert = builder.create();
          alert.show();
  
          }
          catch(Exception e)
          {
      System.out.println("alert="+e);
          }
   }
  </code></pre>
  
  <p>I have added this code in onCreate,</p>
  
  <pre><code> public boolean isNetworkAvailable(Context context) {
  
          boolean value = false;
          ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
                  || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
              value = true;
          }
  
          // Log.d ("1", Boolean.toString(value) );
          return value;
      }
  </code></pre>
  
  <p>When i turn off the internet connection the alert box is showing but while in the middle of my application the internet got slow down or lost connection i am not getting any alert</p>
  
  <p>I need to check every second whether the internet available or not,if no connection i need to show the alert box.</p>
  
  <p>Could somebody help me out @Thanks</p>