title: Android Background Service or Receiver to check the internet connection in the background
body: <p>In my application I am always connected to the openfire server with the help of the internet. Now if the connection is lost in between or disconnected, I want that it should give a popup and on the press of the OK button it should relogin the person( as when the connection to the server is lost - even for 1 secs the person is disconnected).</p>
  
  <p>I m using the following code:</p>
  
  <pre><code>public class ConnectionCheck extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
  
          ConnectivityManager connectivityManager =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
          NetworkInfo mobileNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  
          if(activeNetInfo!=null){
              Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
          }
  
           if( mobileNetInfo != null )
              {
                Toast.makeText( context, "Mobile Network Type : " + mobileNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
              }
  
      }
  </code></pre>
  
  <p>Suppose when wifi is disconnected it connects to the mobile network again( but internally the user is not connected to the server)</p>
  
  <p>What should i do for the same, i.e when the user is disconnected - even for one sec from the server then it should give a popup.</p>
  
  <p>Is it possible??</p>
  
  <p>Thanks</p>