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.
Android device can have only one connection! (WiFi or Mobile)
If WiFi is available - system will try to connect to it
If WiFi is not available and Mobile is turned on - system will try to connect to Mobile
When system is connected to WiFi - You cant' be shure that Mobile will work until you try to connect.
Same for Moble
Related
The following code works fine for Wi-Fi connection:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
However, activeNetworkInfo is always null on an Android 4.2.2 tablet when it is connected to a router via an Ethernet cable. The connection works fine (i.e. all network related functions such as browsing web pages work fine). Could anyone shed some light on this and offer a tip on how to tell whether an Android device is connected to a network reliably?
Given that, i have only WiFi networks available on the device and no mobile networks, only WiFi.
If I cheecked the connectivity to a network using connectivityManager and the result of it is that "I am connected", does that mean I am also connected to a WIFIalso? If yes, why I can not get info about my WiFi such as ssid,speed, link speed, ip and others from the Connectivity Manager object? Because, to have such info you have to be connected to a WiFi first.
You can check if you are connected & connected to a Wifi
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
You can use Wifi Manager to get the information about Wifi Network
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
More info on the WifiInfo
http://developer.android.com/reference/android/net/wifi/WifiInfo.html
I think being connected to a network indicates that you are connected to the internet whether through WiFi, Mobile network, Bluetooth or any other network.
And if you are connected to the internet through a WiFi, then, of course, you have connected to a network but through a specific network.
So, the connectivity manager class, has all the network info about the network through which you are connected to the internet. and since it is a generic class you can obtain info about the network through which you are connected to the internet.
Does anyone know how to programatically detect in Android that wifi is unreachable?
I've got an app running where I am able to easily detect type of network connection and whether is on or not (NetworkInfo class), but I can't detect that wifi is ON, but I am out of reach on the other side of building, so there is no internet. Network info keeps on saying that type of network is WIFI, isConnected = true, getDetailedState = CONNECTED, so these don't help me.
Detect WIFI is connected or on. If wifi is not connected to any INTERNET connection then display message that "WIFI is unreachable or no INTERNET connection."
For checking WIFI is connected use following code.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
I am making an android application. In this application i am receiving data from server. When Wi-Fi is enabled and internet connection is active in our device then it returns the number of record from the server but whenever wifi is enabled but inernet connection is not active or inactive then it gives the force close. I have already checked the wifi connection is enabled.
Please tell me this how can i check that internet is active in our device.
Thanks for advance
Try this code
private boolean isNetworkAvailable()
{
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
I am making an application which makes use of the internet.
It works fine when the Wi-Fi is availble. But when the Wi-Fi is turned off, it gives me an error: "Address family not supported".
I am using the NetworkInfo Class to know, if the network is available. Im using WifiManager to enable and manage Wi-fi connection if available.
I've been using the following code to check if a network is available:
ConnectivityManager mConnectivity = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if (info == null) {
// No inet
}