I had issues with the IsInternetAvailable answer not testing for cellular networks, rather only if wifi was connected. This answer works for both wifi and mobile data:
How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?