5

I want to know how to verify the network connection in Android before sending data ? Can anyone help me ? I try this code but no results

public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) 
    {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
        { 
           for (int i = 0; i < info.length; i++) 
           {
               if (info[i].getState() == NetworkInfo.State.CONNECTED)
               {
                   return true;
               }
           }
        }
    }
    return false;
}
Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
ZACK
  • 55
  • 8
  • I'd do `.getActiveNetworkInfo().isConnected()` like in [this](http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts) question. Also duplicate thereof. – zapl Feb 17 '14 at 22:33

1 Answers1

1

check your manifest :add

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Zied R.
  • 4,964
  • 2
  • 36
  • 67