tags:

views:

47

answers:

1

Hi all,

I want to check the network connectivity in android application.so i inserted the following code

public boolean isNetworkAvailable() {
     Context context = getApplicationContext();
     ConnectivityManager connectivity = (ConnectivityManager) 
        context.getSystemService(Context.CONNECTIVITY_SERVICE);

      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;
    }

when i removed network cable in my computer the program crashed.but when i disable Airplane Mode in Emulator,it correctly shows "NETWORK NOT AVAILABLE". How to we actually check?

A: 

Did you try it on actual device.

My inference from your post is, you are trying on an emulator.
And I think emulator is linked to the network interface of the system
Hence it could have failed.

Other change what I propose :
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info.getState() == NetworkInfo.State.CONNECTED )
return true

Vinay