Created by Administrator on 2016/1/7.
| 9 | * Created by Administrator on 2016/1/7. |
| 10 | */ |
| 11 | public class NetworkUtils { |
| 12 | public static boolean isNetworkAvailable(Context c) { |
| 13 | Context context = c.getApplicationContext(); |
| 14 | // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理) |
| 15 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 16 | |
| 17 | if (connectivityManager == null) { |
| 18 | return false; |
| 19 | } else { |
| 20 | // 获取NetworkInfo对象 |
| 21 | NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo(); |
| 22 | |
| 23 | if (networkInfo != null && networkInfo.length > 0) { |
| 24 | for (NetworkInfo aNetworkInfo : networkInfo) { |
| 25 | // System.out.println(i + "===状态===" + networkInfo[i].getState()); |
| 26 | // System.out.println(i + "===类型===" + networkInfo[i].getTypeName()); |
| 27 | // 判断当前网络状态是否为连接状态 |
| 28 | if (aNetworkInfo.getState() == NetworkInfo.State.CONNECTED) { |
| 29 | return true; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * 判断WIFI是否打开 |
| 39 | * @param context |
| 40 | * @return |
| 41 | */ |
| 42 | public static boolean isWifiEnabled(Context context) { |
| 43 | ConnectivityManager mgrConn = (ConnectivityManager) context |
| 44 | .getSystemService(Context.CONNECTIVITY_SERVICE); |
| 45 | TelephonyManager mgrTel = (TelephonyManager) context |
| 46 | .getSystemService(Context.TELEPHONY_SERVICE); |
| 47 | return ((mgrConn.getActiveNetworkInfo() != null && mgrConn |
| 48 | .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel |
| 49 | .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * 判断是否是3G网络 |
| 54 | * @param context |
| 55 | * @return |
| 56 | */ |
| 57 | public static boolean is3rd(Context context) { |
| 58 | ConnectivityManager cm = (ConnectivityManager) context |
| 59 | .getSystemService(Context.CONNECTIVITY_SERVICE); |
| 60 | NetworkInfo networkINfo = cm.getActiveNetworkInfo(); |
| 61 | if (networkINfo != null |
| 62 | && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) { |
| 63 | return true; |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected