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;
}
Toggle on/off Airplane mode Sometimes, all you need to do when mobile data is on but no internet connection is to turn on and off Airplane mode. Doing so will cut off your phone from the mobile network and can fix the problem alongside it.
A lack of reception in your area is usually what causes “cellular network not available” to appear on your screen. It simply means your phone can't connect to a cellular data signal. This issue could also happen because of the exhaustion or misplacement of your SIM card.
I used the following code to know if I'm connected to the network (no mobile data):
public static Boolean isMobileAvailable(Context appcontext) {
TelephonyManager tel = (TelephonyManager) appcontext.getSystemService(Context.TELEPHONY_SERVICE);
return ((tel.getNetworkOperator() != null && tel.getNetworkOperator().equals("")) ? false : true);
}
The NetworkOperator is returned only if the phone is actually registered to a network.
For me works fine PhoneStateListener:
private static class YourListener extends PhoneStateListener {
@Override
public void onServiceStateChanged(ServiceState serviceState){
if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
// connected to cellular network
}
}
}
And register in TelephonyManager:
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
YourListener listener = new YourListener();
tm.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE);
At registration the telephony manager invokes the onServiceChanged() method on the listener object and passes the current state.
PS: PhoneStateListener uses Handler to post result to UI thread, so you should create it in UI thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With