title: How to check my internet access on Android?
body: <p>What I really want is, when the user has no connection, I want to show some dialog that he or she has no connection. </p>
  
  <p>I tried to put this on my MainActivity but still didn't work.</p>
  
  <pre><code>public boolean isOnline() {
   ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   return cm.getActiveNetworkInfo().isConnectedOrConnecting();
  
  }
  </code></pre>
  
  <p>I also used this one but it didn't also work:</p>
  
  <pre><code>public class ConnectionChangeReceiver extends BroadcastReceiver
      {
        @Override
        public void onReceive( Context context, Intent intent )
        {
          ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
          NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
          NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE );
          if ( activeNetInfo != null )
          {
            Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
          }
          if( mobNetInfo != null )
          {
            Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
          }
        }
      }
  </code></pre>
  
  <p>I added this on my manifest:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  </code></pre>
  
  <p>Can someone tell me what I'm doing wrong, I would really appreciate it a lot. Thanks in advance!</p>