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 below code

```
public static boolean isInternetAvailable(Context context)
    {
        boolean isInternetAvailable = false;

        try
        {  
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

            if(networkInfo != null && (networkInfo.isConnected()))
            {
                isInternetAvailable  = true;
            }    
        }
        catch(Exception exception)
        {
            // Do Nothing
        }

        return isInternetAvailable;
    }

```

and also try on real device

use code in Activity onCreate Method like

```
if(isInternetAvailable(this))
{
Toast toast = Toast.makeText(this, "internet available", Toast.LENGTH_SHORT);
        toast.show();
}
else
{
Toast toast = Toast.makeText(this, "internet not available", Toast.LENGTH_SHORT);
        toast.show();
}

```

Required manifest permission

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

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

```
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"/>

```