I have a function which check network connections using:
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
but, as soon the Wifi network is connected, the mobile network is given as DISCONNECTED
How could I know if both networks are connected?
Thanks in advance for your time.
Try this:
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileNet = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(netInfo!=null){
if(mobileNet.isConnected()){
System.out.println("Mobile Network is connected");
}
if(wifi.isConnected()){
System.out.println("Wi-fi Network is connected");
}
}
How could I know if both networks are connected?
You can create for sure method that will check for both types e.q.
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// do your stuff
}
if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
// do your stuff
}
But in Android OS, if Wifi and Mobile network is available (and you can connect to), OS will choose Wifi by default so it'll use Wifi as active network connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With