android `NetworkInfo`中`isConnected()`和`isAvailable()`之间的区别`
我正在尝试检查设备是否连接到internet。我有下面的实现来实现这一点android `NetworkInfo`中`isConnected()`和`isAvailable()`之间的区别`,android,connectivity,networkinfo,Android,Connectivity,Networkinfo,我正在尝试检查设备是否连接到internet。我有下面的实现来实现这一点 public static boolean isConnectedToNetwork(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwo
public static boolean isConnectedToNetwork(Context context) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
NetworkInfo
提供了两种方法isConnected()
和isAvailable()
。我应该使用哪一种,它们之间的区别是什么
是否有方法检测设备在未连接互联网的情况下连接到Wifi的状态?如果设备连接到网络,isConnected返回true。 如果设备未连接但网络可连接,isAvailable返回true,isConnected返回false 您可以阅读此主题以查找最后一个问题。
如果设备连接到网络,isConnected返回true。 如果设备未连接但网络可连接,isAvailable返回true,isConnected返回false 您可以阅读此主题以查找最后一个问题。
你检查了吗?你做得对。在尝试执行以下操作之前,应调用
isConnected()
connection@aandis是,我已连接。isConnected()
-表示网络连接是否存在。isAvailable()
-表示网络连接是否可能。两个看起来都一样。需要知道确切的区别和用法你检查了吗?你做得对。在尝试执行以下操作之前,应调用isConnected()
connection@aandis是,我已连接。isConnected()
-表示网络连接是否存在。isAvailable()
-表示网络连接是否可能。两个看起来都一样。需要知道确切的区别和用法
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.