package com.fredric.util; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.State; /*- * 网络工具类 */ public class NetUtils { //判断网络是否连接 public static boolean isConnected(Context context){ ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); //WLAN五种状态 //1,显示连接已保存,但标题栏没有,即没有实质连接上,输出为:not connect, available //2,显示连接已保存,标题栏也有已连接上的图标, 输出为:connect, available //3,选择不保存后 输出为:not connect, available //4,选择连接,在正在获取IP地址时 输出为:not connect, not available //5,连接上后 输出为:connect, available if(null != connectivity){ NetworkInfo info = connectivity.getActiveNetworkInfo(); if(null != info && info.isConnected()){ return true; } } return false; } //判断是否wifi连接 public static boolean isWifiConnect(Context context){ ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if(null != connectivity){ State state = connectivity .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); if(state == State.CONNECTED){ return true; } } return false; } //判断GPRS是否连接 public static boolean isGPRSConnect(Context context){ ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if(null != connectivity){ State state = connectivity .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); if(state == State.CONNECTED){ return true; } } return false; } }