Assuming your loop returns the correct values, let's take out the local booleans since they are unnecessary:
private boolean haveNetworkConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI") && ni.isConnected())
return true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE") && ni.isConnected())
return true;
}
return false;
}
If the loop is incorrect, you can incorporate the documentation's method in your code:
private boolean haveNetworkConnection() {
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if(activeNetwork != null)
return activeNetwork.isConnectedOrConnecting();
return false;
}