-2

Possible Duplicate:
how to check wifi or 3g network is available on android device
Check the Internet Connectivity in Android

I am using an Android device and I am disabling the WiFi connection in my device.Now I have an application which uses Internet and whenever the application runs I want to show a simple message i.e a Toast message telling that "No Internet connection" rather than the crashing of the application.

How to do this?

Community
  • 1
  • 1
Jack Dsilva
  • 1,504
  • 3
  • 24
  • 44
  • 3
    Have a look at [this answer](http://stackoverflow.com/questions/5828263/network-connection-always-detected-on-handset-even-with-no-network/5828311#5828311) – Adil Soomro Nov 21 '11 at 11:04
  • Duplicate of http://stackoverflow.com/questions/6094123/check-the-internet-connectivity-in-android and http://stackoverflow.com/questions/6167153/checking-if-programm-has-the-internet-over-wifi – Polynomial Nov 21 '11 at 11:04
  • 4
    As others pointed out, there are already questions with correct answers that can be found within a minute. So **-1** for no research effort. –  Nov 21 '11 at 11:06
  • Interesting how this question came back from -3 in a second. Meanwhile a 8 month old question of mine suddenly got downvoted twice with no reason stated. Very mature. Also what's the point? Want me to downvote without stating a reason in the future so I'm protected from that kind of behaviour? Great. –  Nov 21 '11 at 11:51

2 Answers2

1
 public final boolean isInternetOn()
 {

  ConnectivityManager connec = (ConnectivityManager) getSystemService
  (Context.CONNECTIVITY_SERVICE);

  if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
    ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
    ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
    ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))

  {

 return true;

  }

  else     
  return false;

 }
Hemant Menaria
  • 701
  • 1
  • 7
  • 17
0

Try using this snippet:

    ConnectivityManager cm = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE);
    if( ! ( cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected())  )
    {
        android.util.Log.e(LOGTAG, "No connectivity to send information...");
        return;
    }
Jordi Coscolla
  • 1,066
  • 6
  • 8