判断是否有可用状态的Wifi

5年以前  |  阅读数:470 次  |  编程语言:JAVA 
/**
     * 获取ConnectivityManager
     */
    public static ConnectivityManager getConnManager(Context context) {
        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }


/**
     * 判断是否有可用状态的Wifi,以下情况返回false:
     *  1. 设备wifi开关关掉;
     *  2. 已经打开飞行模式;
     *  3. 设备所在区域没有信号覆盖;
     *  4. 设备在漫游区域,且关闭了网络漫游。
     *  
     * @param context
     * @return boolean wifi为可用状态(不一定成功连接,即Connected)即返回ture
     */
    public static boolean isWifiAvailable(Context context) {
        NetworkInfo[] nets = getConnManager(context).getAllNetworkInfo();
        if (nets != null) {
            for (NetworkInfo net : nets) {
                if (net.getType() == ConnectivityManager.TYPE_WIFI) { return net.isAvailable(); }
            }
        }
        return false;
    }

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8