Android检测wifi网络的连接情况

 1 public static boolean isWifiConnected(Context context)
 2 {
 3     ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
 4     NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
 5     if(wifiNetworkInfo.isConnected())
 6     {
 7         return true ;
 8     }
 9  
10     return false ;
11 }
ConnectivityManager 类中封装了和链接相关的(Wi-Fi, GPRS, UMTS, etc)相关的方法和类,能够检测当前的连接状态。

1  WifiManager wifiMgr = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
2     int state = wifiMgr.getWifiState();
3     if (state == WifiManager.WIFI_STATE_ENABLED || state == WifiManager.WIFI_STATE_ENABLING) {
4     return true;
5     } else {
6     return false;
7     }
This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE). It deals with several categories of items:

The list of configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.
The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.
Results of access point scans, containing enough information to make decisions about what access point to connect to.
It defines the names of various Intent actions that are broadcast upon any sort of change in Wi-Fi state.
This is the API to use when performing Wi-Fi specific operations. To perform operations that pertain to network connectivity at an abstract level, use android.net.ConnectivityManager.

总之  ConnectivityManager主要管理和网络连接相关的操作,而WifiManager主要管理和Wifi相关的操作。

转载于:https://www.cnblogs.com/zhouliweiblog/archive/2012/11/10/2764022.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值