In order check the Internet connection in Android java code:

public boolean checkConnectivity() {
boolean enabled = false;
ConnectivityManager connectivityManager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo wifiConn = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileConn = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
try {
if ((wifiConn.getState() == NetworkInfo.State.CONNECTED)
|| ((mobileConn != null && (mobileConn.getState() == NetworkInfo.State.CONNECTED)))) {
enabled = true;
}
} catch (NullPointerException e) {
}
return enabled;
}