1

I have the code below, which is intended to check if the network being used is Cell or WiFi. If WiFi then return the string WiFi and if cell return the string Cell. The code I have is:

private String checkNetworkState() {
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    //  NetworkInfo mEthernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        NetworkInfo m3G = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (mWifi!=null) isOnWifi = mWifi.isConnected();
        if (m3G!=null) is3G = m3G.isConnected();

        if(isOnWifi == true) {
            return "WiFi";
        }
        else {
            return "Cell";
        }

    }

I have debugged with WiFi enabled. isOnWifi is true and is3G is false. However, both the if AND the else get called, why is this happening?

Having said that the return string I get is actually "WiFi", not Cell. Is this usual?

Dan
  • 2,304
  • 6
  • 42
  • 69
  • Wait, "WiFi" and "Cell" can't be called at the same time, since one is in "else" statement. Or I didn't understood you well? – David Kasabji Dec 12 '14 at 22:13
  • Your question makes no sense- a function can't return twice, and you yourself say there's a return value of wifi. You also say isWifi is true, so that's expected. So what exactly is going on that shouldn't? – Gabe Sechan Dec 12 '14 at 22:28
  • When the method gets called isOnWifi equals true and is3G equals false. So when the if/else block is reached I would expect the return to be ONLY WiFi, however, when putting a debug stop on both the return statements both appear to trigger, which doesn't make any sense. If the IF is triggered surely the ELSE will be bypassed? You're right, however, I do get WiFi as the return value, despite the second return also appearing to get called – Dan Dec 12 '14 at 22:31

1 Answers1

0

if you are checking network state you have to change your method,

private boolean checkNetworkState() {
    ConnectivityManager connManager = (ConnectivityManager)  getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo m3G = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (mWifi!=null) isOnWifi = mWifi.isConnected();
    if (m3G!=null) is3G = m3G.isConnected();

    if(isOnWifi == true || is3G == true) {
        return true;
    }

    return false;
}

you can´t get two values from the same method, try using the respective method for each case:

public boolean isConnectedWifi() {
         ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
         NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
         return mWifi.isConnected();
}

public boolean isConnectedMobile() {
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
         NetworkInfo m3G = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
         return m3G.isConnected();
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268