4

Need to know what kind of value getActiveNetwork returns. code i am using is:

    ConnectivityManager connectivityManager =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);    
    Network network = connectivityManager.getActiveNetwork(); 
    Log.e(TAG,"Network : " + network.toString());

Output is returning number, what that number actually means?

aman arora
  • 263
  • 5
  • 16

7 Answers7

2

Good that many folks mentioned that using connectivityManager.activeNetworkInfo.type or connectivityManager.activeNetworkInfo.typeNameis deprecated for prior to 6.0 SDK version.

I faced similar situation and had to support both minimum 5.0 and >= 6.0 devices. This is what worked for me.

if (SDK_INT >= M) {
        connectivityManager.activeNetwork
                ?.let { connectivityManager.getNetworkCapabilities(it) }
                ?.takeIf { it.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) }
                ?.takeIf { connectivityManager.activeNetworkInfo.isConnected }
                ?.let { return "Wifi is Connected" 
                     //Just to make a point. Better be a sealed class.
                }
    } else {
        connectivityManager.activeNetworkInfo
                ?.takeIf { it.isConnected && it.type == ConnectivityManager.TYPE_WIFI }
                ?.let { return "Wifi is Connected" }
    }

 return "Wifi is Disconnected"
Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41
1

Based on the code for Network class:

@Override
public String toString() { 
    return Integer.toString(netId);
}

Value returned is the Network_Id assigned to default data network.

Sagar
  • 23,903
  • 4
  • 62
  • 62
1

A way to use ConnectivityManager is usually to check if there is currently a network access or not. With getActiveNetworkInfo, you can check if there is a Wifi, Bluetooth or mobile connexion.

For example, this is how you can check if your application has an internet access via wifi or mobile network :

ConnectivityManager connectivityManager =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null 
    || !networkInfo.isConnected() 
    || (networkInfo.getType() != ConnectivityManager.TYPE_WIFI 
    && networkInfo.getType() != ConnectivityManager.TYPE_MOBILE)) {
     // No internet connectivity for any reason
}
user2342558
  • 5,567
  • 5
  • 33
  • 54
Thomas Mary
  • 1,535
  • 1
  • 13
  • 24
0

Instead of getActiveNetwork() use getActiveNetworkInfo(). There you will get a NetworkInfo objects which has a getType() method. In Javadoc you can find all possible values.

MeliX
  • 230
  • 1
  • 6
0

Check android official documentation https://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetwork()

For better result you can use getActiveNetworkInfo. It returns the type of object.Then you can compare it with type of network you want. see that ConnectivityManager getNetworkInfo(int) deprecated

Naveen
  • 350
  • 2
  • 12
0

connectivityManager.getActiveNetwork(); actually returns a Network object corresponding to the currently active default data network.so the number value will be constant value that is assigned to that active network. if you want to check the what type of network is active.i am giving you an example below check that out.

public boolean isNetworkConnected(Context mContext) {
        final String DEBUG_TAG = "NetworkStatusExample";
        ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(CONNECTIVITY_SERVICE);
        boolean isWifiConn=false;
        boolean isMobileConn=false;
        Network nn=connMgr.getActiveNetwork();
        Network[] networkinf=connMgr.getAllNetworks();
        for (int a=0;a<networkinf.length;a++) {
            NetworkCapabilities networkCapabilities = connMgr.getNetworkCapabilities(networkinf[a]);
            if (networkCapabilities.hasTransport(networkCapabilities.TRANSPORT_CELLULAR)){
                isMobileConn=true;
            }
            else if (networkCapabilities.hasTransport(networkCapabilities.TRANSPORT_WIFI)){
                isWifiConn=true;
            }
        }
        Log.i(DEBUG_TAG, "Wifi connected: " + isWifiConn);
        Log.i(DEBUG_TAG, "Mobile connected: " + isMobileConn);
        editor.putBoolean("isConnected",isMobileConn);
        editor.apply();
        if (isMobileConn || isWifiConn){
            Log.i(DEBUG_TAG, "Network Status..." + isMobileConn);
            return true;
        }
        else {
            return false;
        }
    }
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
-1

It returns network status like the network is available or not. If you want network info then you can use getActiveNetworkInfo(). It will provide you the information like this:

NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "WIFI_NAME", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false, simId: 0