Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way of checking for mobile network available (no data connection)

Tags:

android

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;
}
like image 511
denis Avatar asked Jun 22 '11 06:06

denis


People also ask

What to do if there is no data connection?

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.

Why does my phone say no data network available?

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.


2 Answers

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.

like image 60
Santacrab Avatar answered Sep 23 '22 01:09

Santacrab


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.

like image 41
Viktor Avatar answered Sep 22 '22 01:09

Viktor