title: Android - Programmatically check internet connection and display dialog if notConnected
body: <p>I am working on a live project. and when user click on the app. the welcome screen appears(there is a webview on that screen). and if the internet is not connected then the app crashes. Basically, my problem is to check programmatically that is mobile is connected to internet or not. if not then don't fetch the data from webservice into webview and display a dialog box showing "Check your internet connection" </p>
  
  <p>while doing research i found many things, and i have tried to implement that. but, its not satisfying my requirement</p>
  
  <p>my code is,</p>
  
  <pre><code>public boolean isOnline() {
      ConnectivityManager cm =
          (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo netInfo = cm.getActiveNetworkInfo();
      if (netInfo != null &amp;&amp; netInfo.isConnectedOrConnecting()) {
          return true;
      }
      else
      {
          Description.setVisibility(View.INVISIBLE);
          new AlertDialog.Builder(WelcomePage.this)
          .setTitle(getResources().getString(R.string.app_name))
          .setMessage(
                  getResources().getString(
                          R.string.internet_error))
          .setPositiveButton("OK", null).show();
      }
      return false;
  }
  </code></pre>
  
  <p>i am calling this function in <code>doInBackground()</code> of <code>AsyncTask</code></p>
  
  <p>Please Help!</p>