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
}
Related
I am using this code to check the connection in my app :
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
this method will return "true" even the device just connect to wifi and not has a connection to the internet or the device turn on mobile network but run out of data. I have read many posts, but most of them are too old. I dont know those answers are still right or not. So, how to check if the device has a "real" connection to the internet ?
This maybe a similar question : Android: Check network and real internet connectivity
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.
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
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
}
One of the major requirments of my application is using cellular internet connection only.
In case of detecting wifi connection, the application have to disable the wifi ability and connecting through the cellualr internet connection.
How it can be done?
Thanks,
Eyal.
Checking WIFI connection
You can use the following code to check if the active network (if any - do some null checks here) is of type WIFI.
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connManager.getActiveNetworkInfo();
if (netInfo!=null) {
// detect type using netInfo.getType()
}
There's a TYPE_WIFI int constant in ConnectivityManager that you can use for your check. (getType returns an int to identify the network type).
More info on the Activity Manager can be found on the Android dev site.
Disabling WIFI
n order to disable the WiFi state, you have to grant the following permission in the application manifest
android.permission.CHANGE_WIFI_STATE
You can then use the WifiManager to set enable/disable the WIFI.
WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
More info on http://developer.android.com/reference/android/net/wifi/WifiManager.html
For a complete sample, check the following post : http://android-er.blogspot.com/2011/01/turn-wifi-onoff-using.html