Reputation: 3574
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?
Upvotes: 1
Views: 6644
Reputation: 766
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.
Upvotes: 2
Reputation: 41
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
Upvotes: 4