to check whether your phone is connected to the Internet, use the following code:
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
        boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
        boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();

these lines check for internet connection whether it is through WiFi or Mobile Internet.

you need to add the following permission to the AndroidManifest.xml file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Another method is what our freind Kevin mentioned in the comments, is to check if a certain website is reachable or not like this:
public static boolean isOnline() {
try {
InetAddress.getByName("google.ca").isReachable(3);
return true;
} catch (UnknownHostException e){
return false;
} catch (IOException e){
return false;
}
}

we check if a site is reachable within a certain timeout or not.