android 中判断WiFi是否可用的可靠方法
2011-08-27 22:20
651 查看
在一些程序中,需要从网上下载数据,或者通过其他方式对网络产生流量,当wifi不可用时应该提示用户wifi已经不可用了,是否继续,因为如果wifi掉了,那么程序可能采用3G卡或其他的收费的渠道使用网络,会导在不知情时产生大量的上网费用。通过查看android的api可使用下列方法进行判断:
在模拟器上使用这个方法时,可以正确判断wifi是否可用,但是在真机上就判断不出来。wifi是断开的,但是返回的结果true,造成wifi判断不准确。经过尝试可使用如下的方法判断方能正确:
public static boolean isWiFiActive(Context inContext) { Context context = inContext.getApplicationContext(); WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); return wifiManager.isWifiEnabled(); }
在模拟器上使用这个方法时,可以正确判断wifi是否可用,但是在真机上就判断不出来。wifi是断开的,但是返回的结果true,造成wifi判断不准确。经过尝试可使用如下的方法判断方能正确:
public static boolean isWiFiActive(Context inContext) { Context context = inContext.getApplicationContext(); ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) { return true; } } } } return false; }
相关文章推荐
- android 中判断WiFi是否可用的可靠方法 ,android 是否联网
- android 中判断WiFi是否可用的可靠方法
- android 中判断WiFi是否可用的“可靠”方法
- android 中判断WiFi是否可用的可靠方法 ,android 是否联网
- Android编程判断网络是否可用及调用系统设置项的方法
- Android--用ping的方法判断当前网络是否可用
- Android 判断网络是否可用、网络类型WIFI/2G/3G/4G及获取IP
- android判断网络是否可用方法
- Android判断是否有可用网络和判断WIFI是否ON
- Android判断网络连接是否可用,WiFi、移动数据是否打开等
- android 判断当前网络是否可用(ping网络),包含wifi和移动数据网络
- Android 判断开启网络,wifi是否可用
- Android 用ping的方法判断当前网络是否可用
- Android判断网络是否可用的方法
- Android中判断有无可用网络(是否是3G或者WIFI网络)
- Android开发-常用工具方法(dp转成px、网络是否可用、是否3G网络、Gps是否打开、判断手机号码等)
- Android 检查WIFI是否可用的实现方法
- Android判断GPS、wifi、移动网络是否可用的探知函数
- Android判断定位功能是否可用的方法