zoukankan      html  css  js  c++  java
  • 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相关的操作。

  • 相关阅读:
    模块模式——属性
    防止变量被覆盖
    自执行匿名函数语法和普通函数语法对比
    JavaScript更改原型
    JavaScript覆盖原型以及更改原型
    JavaScript原型链
    作用域链和原型链描述javaScript访问变量和属性的顺序
    javascript高级变量提升和执行环境对象
    构建第一个单页应用
    error: expected identifier before numeric constant 问题
  • 原文地址:https://www.cnblogs.com/zhouliweiblog/p/2764022.html
Copyright © 2011-2022 走看看