码迷,mamicode.com
首页 > 移动开发 > 详细

Android 开发工具类 18_NetWorkUtil

时间:2015-05-29 11:34:13      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

检测网络的一个工具包:

1、网络是否可用;

2、判断是否有网络连接;

3、判断 WIFI 网络是否可用;

4、判断 MOBILE 网络是否可用;

5、获取当前网络连接的类型信息;

6、获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap 网络3:net网络;

  1 import android.content.Context;
  2 import android.net.ConnectivityManager;
  3 import android.net.NetworkInfo;
  4 
  5 /**
  6  * @Title NetWorkUtil
  7  * @Description 是检测网络的一个工具包
  8  * @author 淡定
  9  */
 10 public class NetWorkUtil {
 11     
 12     public static enum NetType {
 13         WIFI, CMNET, CMWAP, NoneNet
 14     }
 15 
 16     /**
 17      * 网络是否可用
 18      * 
 19      * @param context
 20      * @return
 21      */
 22     public static boolean isNetworkAvailable(Context context) {
 23         
 24         ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 25         NetworkInfo[] info = mgr.getAllNetworkInfo();
 26         if (info != null) {
 27             for (int i = 0; i < info.length; i++) {
 28                 if (info[i].getState() == NetworkInfo.State.CONNECTED) {
 29                     return true;
 30                 }
 31             }
 32         }
 33         return false;
 34     }
 35 
 36     /**
 37      * 判断是否有网络连接
 38      * 
 39      * @param context
 40      * @return
 41      */
 42     public static boolean isNetworkConnected(Context context) {
 43         if (context != null) {
 44             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 45             NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
 46             if (mNetworkInfo != null) {
 47                 return mNetworkInfo.isAvailable();
 48             }
 49         }
 50         return false;
 51     }
 52 
 53     /**
 54      * 判断WIFI网络是否可用
 55      * 
 56      * @param context
 57      * @return
 58      */
 59     public static boolean isWifiConnected(Context context) {
 60         
 61         if (context != null) {
 62             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 63             NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
 64             if (mWiFiNetworkInfo != null) {
 65                 return mWiFiNetworkInfo.isAvailable();
 66             }
 67         }
 68         return false;
 69     }
 70 
 71     /**
 72      * 判断 MOBILE 网络是否可用
 73      * 
 74      * @param context
 75      * @return
 76      */
 77     public static boolean isMobileConnected(Context context) {
 78         if (context != null) {
 79             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 80             NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
 81             if (mMobileNetworkInfo != null) {
 82                 return mMobileNetworkInfo.isAvailable();
 83             }
 84         }
 85         return false;
 86     }
 87 
 88     /**
 89      * 获取当前网络连接的类型信息
 90      * 
 91      * @param context
 92      * @return
 93      */
 94     public static int getConnectedType(Context context) {
 95         
 96         if (context != null) {
 97             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 98             NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
 99             if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
100                 return mNetworkInfo.getType();
101             }
102         }
103         return -1;
104     }
105 
106     /**
107      * 获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap 网络3:net网络
108      * 
109      * @param context
110      * @return
111      */
112     public static NetType getAPNType(Context context) {
113         
114         ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
115         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
116         if (networkInfo == null) {
117             return NetType.NoneNet;
118         }
119         int nType = networkInfo.getType();
120 
121         if (nType == ConnectivityManager.TYPE_MOBILE) {
122             if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
123                 return NetType.CMNET;
124             } else {
125                 return NetType.CMWAP;
126             }
127         } else if (nType == ConnectivityManager.TYPE_WIFI) {
128             return NetType.WIFI;
129         }
130         return NetType.NoneNet;
131 
132     }
133 }

 

Android 开发工具类 18_NetWorkUtil

标签:

原文地址:http://www.cnblogs.com/renzimu/p/4537936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!