|
| 1 | +package com.chengxinet.bobo.utils; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.net.ConnectivityManager; |
| 5 | +import android.net.NetworkInfo; |
| 6 | +import android.telephony.TelephonyManager; |
| 7 | + |
| 8 | +/** |
| 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 | + /** |
| 69 | + * 判断是wifi还是3g网络 |
| 70 | + * @param context |
| 71 | + * @return |
| 72 | + */ |
| 73 | + public static boolean isWifi(Context context) { |
| 74 | + ConnectivityManager cm = (ConnectivityManager) context |
| 75 | + .getSystemService(Context.CONNECTIVITY_SERVICE); |
| 76 | + NetworkInfo networkINfo = cm.getActiveNetworkInfo(); |
| 77 | + if (networkINfo != null |
| 78 | + && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + return false; |
| 82 | + } |
| 83 | +} |
0 commit comments