What is the correct way of checking if a mobile network (GSM) connection is available on Android? (>2.1) I don't want to check if there is data connection available over the mobile network just check for network availability in general. (check if phone calls over the mobile network are possible)
At the moment I am using the following check:
public static boolean checkMobileNetworkAvailable(Context context){
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = tm.getNetworkType();
return (networkType != TelephonyManager.NETWORK_TYPE_UNKNOWN);
}
But it seems that some devices always report back "NETWORK_TYPE_UNKNOWN". So the check fails all the time.
Is there a better approach of doing it?
Update:
Would the following approach be better?
public static boolean checkMobileNetworkAvailable(Context context){
boolean isMobileNetworkAvailable = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networks = cm.getAllNetworkInfo();
for(int i=0;i<networks.length;i++){
if(networks[i].getType() == ConnectivityManager.TYPE_MOBILE){
if(networks[i].isAvailable()){
isMobileNetworkAvailable = true;
}
}
}
return isMobileNetworkAvailable;
}