Difference between `isConnected()` and `isAvailable()` in android `NetworkInfo` - android

I'm trying to check if the device is connected to internet or not. I have the below implementation to do that
public static boolean isConnectedToNetwork(Context context) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
NetworkInfo provides two methods isConnected() and isAvailable(). Which one should I use and what is the difference between them.
And is there a way to detect the state where the device is connected to Wifi without an internet connection?

If the device is connected to a network, isConnected returns true.
If the device is not connected but network is available to connect, isAvailable returns true, isConnected returns false.
You can read this topic for find your last question.
Android Check if there is WiFi but no internet

isConnected()
Indicates whether network connectivity exists and it is possible to establish connections and pass data.
- Always call this before attempting to perform data transactions.
isAvailable()
Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include
- The device is out of the coverage area for any network of this type.
- The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled.
- The device's radio is turned off, e.g., because airplane mode is enabled.
Reference Link

Related

Is there a listener for when a user gets connected to a WiFi network (with internet access)?

I'm struggling to get to know when a user gets connected to a WiFi network with Internet access.
So far, I've tried two ways to achieve that:
using a BroadcastReceiver (action: android.net.conn.CONNECTIVITY_CHANGE), and;
registering a NetworkCallback on ConnectivityManager.
Both of them fail most of the times since their callbacks are called too soon, because they are called while the network has no Internet access.
Is there a way to achieve what I want (for APIs >= 16)?
What I will suggest is to use ConnectivityManager to monitor connectivity status by implementing the following codes
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnected();
For more information please check the android Developer guide here

how to check if a device has a "real" internet connection on main thread?

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

what does "isRoaming" in the Wifip2p networkinfo mean?

What is the meaning of isRoaming in Wifip2p network info mean?
[http://developer.android.com/reference/android/net/NetworkInfo.html#isRoaming()]
It tells that usage of data on that network may incur extra charges? Which data is it refering to and how the usage will lead to extra charges?
Well, if you're outside your home country/state and you turn on the phone it will automatically establish a GSM connection to the roaming partner network -> TelephonyManager.isNetworkRoaming() will return true. If data roaming is disabled or 'use only 2g' is enabled NetworkInfo.isRoaming() will return false, because no data connection is established. If you switch data services on NetworkInfo.isRoaming() will return true as well, since now both (GSM and data connection) are established and in roaming mode.
Hence, setting data-roaming on/off will make NetworkInfo.isRoaming() return true/false.
UPD: It is used when one wants to know whether the user has enabled Data on Roaming while on 2G/3G network.
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
//[edit: check for null]
if(ni != null) {
//...Here check if this is connected and available...
if (ni.isRoaming())
{
// user has enabled data even while he is on Roaming!!
}
}
Since he asked specifically about Wifi P2P the correct answer that this will be an unused field as roaming only applied to cellular network and does not apply to Wifi P2P network information.
The only fields of networkinfo you should be using concerning wifi P2P is: isConnected (after which you normally request the connection info from the wifip2pmanager).

Android is device connected to the internet method

I want to create a method which tells if the device is online. As far as I understand ConnectivityManager tells only if the device is connected to a network. This doesn't mean that the device is connected to the internet. To ensure that the device is online I'm using InetAddress.getByName("google.com").isReachable(3); but I can't use it on the main thread. I can create a separate thread to check the connectivity and then use a callback function but is there another way? I don't want my app to do anything before it is connected. Do you have any solutions? Thank you!
With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute
try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}

Android terminate wifi ability within application

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

Categories

Resources