title: ANDROID Show a dialog when the Internet/GPS loses connectivity or is not connected
body: <p>[EDITED] </p>
  
  <p>I want to show a Splash Screen / dialog when the Internet or GPS is down or not connected so the user can't use the app until the connection is good again. </p>
  
  <p>I tried with some broadcast receivers, but all had problems. One of them:</p>
  
  <pre><code>public class ConnectivityChangeReceiver extends BroadcastReceiver {
  
  @Override
  public void onReceive(final Context context, final Intent intent) {
  
      String status = NetworkUtil.getConnectivityStatusString(context);
  
      Toast.makeText(context, status, Toast.LENGTH_LONG).show();
      if(status.equals("Not connected to Internet")) {
          Intent splashIntent = new Intent(context, SplashScreen.class);
          splashIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(splashIntent);
   //I also had a Toast for internet is connected
      }
  
  }
  }
  </code></pre>
  
  <p>And the util class:</p>
  
  <pre><code>public class NetworkUtil {
  
  public static int TYPE_WIFI = 1;
  public static int TYPE_MOBILE = 2;
  public static int TYPE_NOT_CONNECTED = 0;
  
  
  public static int getConnectivityStatus(Context context) {
      ConnectivityManager cm = (ConnectivityManager) context
              .getSystemService(Context.CONNECTIVITY_SERVICE);
  
      NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
      if (null != activeNetwork) {
          if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
              return TYPE_WIFI;
  
          if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
              return TYPE_MOBILE;
      }
      return TYPE_NOT_CONNECTED;
  }
  
  public static String getConnectivityStatusString(Context context) {
      int conn = NetworkUtil.getConnectivityStatus(context);
      String status = null;
      if (conn == NetworkUtil.TYPE_WIFI) {
          status = "Wifi enabled";
      } else if (conn == NetworkUtil.TYPE_MOBILE) {
          status = "Mobile data enabled";
      } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
          status = "Not connected to Internet";
      }
      return status;
  }
  }
  </code></pre>
  
  <p>Problems: it doesn't work instantly. I usually get 5 toasts in a row when the internet is disconnected and after some time if I modify the status the connection toast stop appearing or appear after a long wait of time.</p>
  
  <p>Also, how to close the Splash Screen activity when the internet is back up? </p>