Question: I'm trying to check network available or not.  
But in my emulator it always shows `connected` (after disconnecting internet also)

Is there any mistake in my code.I am connecting with **wifi**.

*Code:*

```
public class AndroidConnectivityActivity extends Activity
{
Button checkBtn;

/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
      checkBtn = (Button) findViewById(R.id.button1);

      checkBtn.setOnClickListener(new View.OnClickListener()
      {
       public void onClick(View v)
       {
        if(isInternetOn())
        {
         Toast.makeText(getBaseContext(), "Connected",
           Toast.LENGTH_SHORT).show();
        }
        else
        {
         Toast.makeText(getBaseContext(), "Not connected",
           Toast.LENGTH_SHORT).show();
        }
       }});
     }

     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 if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED)
        || (connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED))
      {
       return false;
      }

      return false;
     }
    }

```

Answer: 
Use mehod shoen below 

```
public static boolean isInternetAvailable(Context context) {
            ConnectivityManager cm =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                return true;
            }
            return false;
        }

```

and in manifest set permission as 

```
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

```
use this:

```
private boolean getOnlineState() {
        ConnectivityManager myConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo myNetworkInfo = myConnectivityManager
                .getActiveNetworkInfo();
        if (myNetworkInfo != null
                && myNetworkInfo.isConnectedOrConnecting())
            return true;
        return false;

    }

```

and btw:

```
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 if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED)
    || (connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED))
  {
   return false;
  }

  return false;
 }

```

is the same like:

```
    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;
         return false;

```

Yous should try such hardwareoperations on a real phone rather then the emulator.