Reputation: 273
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?
Upvotes: 4
Views: 2042
Reputation: 1579
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
}
Upvotes: 1
Reputation: 4385
Good that many folks mentioned that using connectivityManager.activeNetworkInfo.type
or connectivityManager.activeNetworkInfo.typeName
is 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"
Upvotes: 2
Reputation: 1
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;
}
}
Upvotes: 0
Reputation: 24907
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.
Upvotes: 1
Reputation: 360
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
Upvotes: 0
Reputation: 52
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
Upvotes: -1
Reputation: 240
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.
Upvotes: 0