programmatically check android phone connected to internet

To detect Android phone is connected to internet or not programmatically use the following sample code.
To identify the mobile network state, use ConnectivityManager class, first get the object and identify the state i.e is it Mobile data (2G/3G) or Wifi broadband internet.Check the connectivitymanager object is returning NULL or not, i have seen in few of the tablets like Samsung or Nexus they returns NULL. So its good to check & proceed.

 public static boolean isInternetconnected(Context ct)  
   {  
    boolean connected = false;  
       //get the connectivity manager object to identify the network state.  
    ConnectivityManager connectivityManager = (ConnectivityManager)ct.getSystemService(Context.CONNECTIVITY_SERVICE);  
       //Check if the manager object is NULL, this check is required. to prevent crashes in few devices.
    if(connectivityManager != null)  
    {  
         //Check Mobile data or Wifi net is present  
      if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||   
          connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)   
      {  
        //we are connected to a network  
        connected = true;  
      }  
      else  
      {  
        connected = false;  
      }  
      return connected;  
    }  
    else  
    {  
    return false;  
    }  
   }