17

Assuming that both WIFI and Data/3G are enabled on a device, how do I check if the user is currently using the internet over the wifi or the data plan assuming they are both enabled. So I don't need to check if they are enabled or disabled, I would like to detect which one is the user actually using.

I've been doing the following, is this the only solution?

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) {
    String ssid = wifiInfo.getSSID();
}
ARMAGEDDON
  • 939
  • 3
  • 11
  • 23

4 Answers4

30
void chkStatus() {
    final ConnectivityManager connMgr = (ConnectivityManager)
    this.getSystemService(Context.CONNECTIVITY_SERVICE);
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifi.isConnectedOrConnecting ()) {
        Toast.makeText(this, "Wifi", Toast.LENGTH_LONG).show();
    } else if (mobile.isConnectedOrConnecting ()) {
        Toast.makeText(this, "Mobile 3G ", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show();
    }
}
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
  • what is this downvote for please comment so I can work on my answers as far as I know I am very much correct – Aashish Bhatnagar Jun 17 '13 at 13:29
  • 4
    If it's already been answered, please link to the question rather than plagiarizing the answer. Certainly the answer is correct -- you copied it, after all. However, that just create duplicates on SO, making SO overall less usable. – 323go Jun 17 '13 at 13:30
  • 1
    Your answer is wrong. isAvailable() tells if network connectivity via wifi or mobile is possible, but don't tell if there is actual connection. You must check isConnectedOrConnecting() or just isConnected(). – Andranik May 27 '15 at 09:09
  • thanks for pointing this out, I will update my answer but I believe it solved the ARMAGEDDON's problem. – Aashish Bhatnagar May 29 '15 at 09:33
  • 2
    This solution is deprecated, see https://stackoverflow.com/questions/32547006/connectivitymanager-getnetworkinfoint-deprecated for the new solution – RoaflinSabos Sep 15 '17 at 12:56
  • Can someone tell why they are using two variable for one condition. Don't you think it could be simply check with one variable like this way `android.net.NetworkInfo internet = manager.getActiveNetworkInfo(); if( internet.getType() == ConnectivityManager.TYPE_WIFI) { networkStatus = "wifi"; }else if(internet.getType() == ConnectivityManager.TYPE_MOBILE){ networkStatus = "mobileData"; }` – badarshahzad Aug 18 '18 at 18:32
  • Both "mobile" and "wifi" variables can be null, so asking for "isConnectedOrConnecting()" could raise a NPException. Keeping that in mind this should be the accepted solution. – Federico Alvarez Jan 30 '20 at 20:53
  • Hey this one helped , And how do we detect any changes, like user switch from wifi to mobile data, any Listers for this? – Jithin Angel'z Jas'z Sep 26 '21 at 14:49
6

You can use the following method to check network status every time:

public static String checkNetworkStatus(final Context context) {

        String networkStatus = "";

        // Get connect mangaer
        final ConnectivityManager connMgr = (ConnectivityManager)  
                context.getSystemService(Context.CONNECTIVITY_SERVICE);

        // check for wifi
        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        // check for mobile data
        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if( wifi.isAvailable() ) {
            networkStatus = "wifi";
        } else if( mobile.isAvailable() ) {
            networkStatus = "mobileData";
        } else {
            networkStatus = "noNetwork";
        }

        return networkStatus;

}  // end checkNetworkStatus 

//vKj

William
  • 2,917
  • 5
  • 30
  • 47
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
0
 public boolean CheckConnectivity(final Context c) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (mConnectivityManager.getActiveNetworkInfo() != null
                && mConnectivityManager.getActiveNetworkInfo().isAvailable()
                && mConnectivityManager.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            return false; // make it false
        }
    }
bummi
  • 27,123
  • 14
  • 62
  • 101
  • 1
    This is to check if Internet is connected or not , It won't tell if you are connected with wifi or mobile data – Manohar Aug 20 '18 at 06:19
0

getNetworkInfo is a deprecated method. I suggest to use getActiveNetworkInfo

Example

private String checkNetworkStatus(Context context) {

    String networkStatus = "";
    final ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if( manager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
        networkStatus = "wifi";
    } else if(manager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_MOBILE){
        networkStatus = "mobileData";
    } else{
        networkStatus="noNetwork";
    }
    return networkStatus;
}
Alp Altunel
  • 3,324
  • 1
  • 26
  • 27
Posa
  • 53
  • 9