Android网络相关工具类

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

    public class NetworkUtils {  
        // 判断当前网络是否可用  
        public static boolean networkIsConnect(Context context) {  
            ConnectivityManager conn = (ConnectivityManager) context  
                    .getSystemService(Context.CONNECTIVITY_SERVICE);  
            NetworkInfo info = conn.getActiveNetworkInfo();  
            if (info != null && info.isConnected()) {  
                return true;  
            } else {  
                return false;  
            }  
        }  
      
        // 判断当前连接的网络是否是wifi,并得到连接当前Wifi的信息  
        public static boolean networkIsWifi(Context context) {  
            ConnectivityManager conn = (ConnectivityManager) context  
                    .getSystemService(Context.CONNECTIVITY_SERVICE);  
            NetworkInfo info = conn.getActiveNetworkInfo();  
            if (info != null && info.isConnected()  
                    && info.getType() == ConnectivityManager.TYPE_WIFI) {  
                WifiManager wifiManager = (WifiManager) context  
                        .getSystemService(Context.WIFI_SERVICE);  
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
                Toast.makeText(context, "连接的wifi网络的id为:"+wifiInfo.getNetworkId(), Toast.LENGTH_SHORT).show();  
                return true;  
            } else {  
                return false;  
            }  
        }  
      
        // 是否打开Wifi  
        public static void setWifiEnabled(Context context, boolean enabled) {  
            WifiManager wifiManager = (WifiManager) context  
                    .getSystemService(Context.WIFI_SERVICE);  
            if (enabled) {  
                wifiManager.setWifiEnabled(true);  
            } else {  
                wifiManager.setWifiEnabled(false);  
            }  
        }  
      
    }