├── README.md
├── NetUtil.kt
└── NetUtil.java
/README.md:
--------------------------------------------------------------------------------
1 | # NetUtil
2 | ### 参考谷歌官方文档,Android 监听网络变化、判断网络连接类型、网络制式等工具类
3 | #### 使用示例:
4 | ##### 1、判断网络是否连接
5 | ```java
6 | public void isNetConnected(View view) {
7 | boolean netConnected = NetUtil.isNetConnected(this);
8 | }
9 | ```
10 |
11 | ##### 2、判断是否移动网络连接
12 | ```java
13 | public void isMobileConnected(View view) {
14 | boolean mobileConnected = NetUtil.isMobileConnected(this);
15 | }
16 | ```
17 |
18 | ##### 3、判断是否移动2G网络连接
19 | ```java
20 | public void is2GConnected(View view) {
21 | boolean 2GConnecte = NetUtil.is2GConnected(this);
22 | }
23 | ```
24 |
25 | ##### 4、判断是否移动3G网络连接
26 | ```java
27 | public void is3GConnected(View view) {
28 | boolean 3GConnecte = NetUtil.is3GConnected(this);
29 | }
30 | ```
31 |
32 | ##### 5、判断是否移动4G网络连接
33 | ```java
34 | public void is4GConnected(View view) {
35 | boolean 4GConnecte = NetUtil.is4GConnected(this);
36 | }
37 | ```
38 |
39 | ##### 6、获取移动网络运营商名称
40 | ```java
41 | public void getNetworkOperatorName(View view) {
42 | String getNetworkOperatorName = NetUtil.getNetworkOperatorName(this);
43 | }
44 | ```
45 |
46 | ##### 7、获取移动终端类型
47 | ```java
48 | public void getPhoneType(View view) {
49 | String phoneType = NetUtil.getPhoneType(this);
50 | }
51 | ```
52 |
53 | ##### 8、判断是否Wifi连接
54 | ```java
55 | public void isWifiConnected(View view) {
56 | boolean wifiConnected = NetUtil.isWifiConnected(this);
57 | }
58 | ```
59 |
60 | ##### 9、注册网络变化监听
61 | ```java
62 | public void registerNetConnChangedReceiver(View view) {
63 | NetUtil.registerNetConnChangedReceiver(this);
64 | }
65 | ```
66 |
67 | ##### 10、移除注册网络变化监听
68 | ```java
69 | public void unregisterNetConnChangedReceiver(View view) {
70 | NetUtil.unregisterNetConnChangedReceiver(this);
71 | }
72 | ```
73 |
74 | ##### 11、添加网络变化监听
75 | ```java
76 | public void addNetConnChangedListener(View view) {
77 | NetUtil.addNetConnChangedListener((connectStatus) -> Log.e("##", "connectStatus: " + connectStatus));
78 | }
79 | ```
80 |
81 | ##### 12、移除网络变化监听
82 | ```java
83 | public void removeNetConnChangedListener(View view) {
84 | NetUtil.removeNetConnChangedListener(listener));
85 | }
86 | ```
87 |
88 |
89 |
--------------------------------------------------------------------------------
/NetUtil.kt:
--------------------------------------------------------------------------------
1 | package com.tw.utils
2 |
3 | import android.content.BroadcastReceiver
4 | import android.content.Context
5 | import android.content.Intent
6 | import android.content.IntentFilter
7 | import android.net.ConnectivityManager
8 | import android.net.NetworkInfo
9 | import android.telephony.TelephonyManager
10 | import android.util.Log
11 | import com.ids.base.utils.BuildConfig
12 |
13 | /**
14 | * 网络工具类
15 | * 参考{@url https://developer.android.com/training/basics/network-ops/managing.html}
16 | * 需要权限:
17 | *
{@link Manifest.permission#ACCESS_NETWORK_STATE}
18 | * ConnectivityManager: Answers queries about the state of network connectivity. It also notifies applications when network connectivity changes.
19 | * NetworkInfo: Describes the status of a network interface of a given type (currently either Mobile or Wi-Fi).
20 | */
21 | class NetUtil private constructor() {
22 | companion object {
23 | private const val TAG = "NetUtil"
24 | private val D = BuildConfig.DEBUG
25 | private val sNetConnChangedReceiver = NetConnChangedReceiver()
26 | private val sNetConnChangedListeners = ArrayList()
27 |
28 | interface NetConnChangedListener {
29 | fun onNetConnChanged(connectStatus: ConnectStatus)
30 | }
31 |
32 | enum class ConnectStatus {
33 | NO_NETWORK,
34 | WIFI,
35 | MOBILE,
36 | MOBILE_2G,
37 | MOBILE_3G,
38 | MOBILE_4G,
39 | MOBILE_UNKNOWN,
40 | OTHER,
41 | NO_CONNECTED
42 | }
43 |
44 | /**
45 | * 网络接口是否可用(即网络连接是否可行)和/或连接(即是否存在网络连接,是否可以建立套接字并传递数据)
46 | *
47 | * @param context 上下文
48 | * @return {@code true} 网络可用
49 | */
50 | fun isNetConnected(context: Context): Boolean {
51 | val activeInfo = getActiveNetworkInfo(context)
52 | return activeInfo?.isConnected ?: false
53 | }
54 |
55 | /**
56 | * 是否移动数据连接
57 | *
58 | * @param context 上下文
59 | * @return {@code true} 移动数据连接
60 | */
61 | fun isMobileConnected(context: Context): Boolean {
62 | val activeInfo = getActiveNetworkInfo(context)
63 | return activeInfo?.run { isConnected && type == ConnectivityManager.TYPE_MOBILE } ?: false
64 | }
65 |
66 | /**
67 | * 是否2G网络连接
68 | *
69 | * @param context 上下文
70 | * @return {@code true} 2G网络连接
71 | */
72 | fun is2GConnected(context: Context): Boolean {
73 | if (!isNetConnected(context)) {
74 | return false
75 | }
76 | val activeInfo = getActiveNetworkInfo(context)
77 | val subtype = activeInfo?.subtype
78 | return when (subtype) {
79 | TelephonyManager.NETWORK_TYPE_GPRS,
80 | TelephonyManager.NETWORK_TYPE_GSM,
81 | TelephonyManager.NETWORK_TYPE_EDGE,
82 | TelephonyManager.NETWORK_TYPE_CDMA,
83 | TelephonyManager.NETWORK_TYPE_1xRTT,
84 | TelephonyManager.NETWORK_TYPE_IDEN -> true
85 | else -> false
86 | }
87 | }
88 |
89 | /**
90 | * 是否3G网络连接
91 | *
92 | * @param context 上下文
93 | * @return {@code true} 3G网络连接
94 | */
95 | fun is3GConnected(context: Context): Boolean {
96 | if (!isNetConnected(context)) {
97 | return false
98 | }
99 | val activeInfo = getActiveNetworkInfo(context)
100 | val subtype = activeInfo?.subtype
101 | return when (subtype) {
102 | TelephonyManager.NETWORK_TYPE_UMTS,
103 | TelephonyManager.NETWORK_TYPE_EVDO_0,
104 | TelephonyManager.NETWORK_TYPE_EVDO_A,
105 | TelephonyManager.NETWORK_TYPE_HSDPA,
106 | TelephonyManager.NETWORK_TYPE_HSUPA,
107 | TelephonyManager.NETWORK_TYPE_HSPA,
108 | TelephonyManager.NETWORK_TYPE_EVDO_B,
109 | TelephonyManager.NETWORK_TYPE_EHRPD,
110 | TelephonyManager.NETWORK_TYPE_HSPAP,
111 | TelephonyManager.NETWORK_TYPE_TD_SCDMA -> true
112 | else -> false
113 | }
114 | }
115 |
116 | /**
117 | * 是否4G网络连接
118 | *
119 | * @param context 上下文
120 | * @return {@code true} 4G网络连接
121 | */
122 | fun is4GConnected(context: Context): Boolean {
123 | if (!isNetConnected(context)) {
124 | return false
125 | }
126 | val activeInfo = getActiveNetworkInfo(context)
127 | val subtype = activeInfo?.subtype
128 | return when (subtype) {
129 | TelephonyManager.NETWORK_TYPE_LTE,
130 | TelephonyManager.NETWORK_TYPE_IWLAN -> true
131 | else -> false
132 | }
133 | }
134 |
135 | /**
136 | * 获取移动网络运营商名称
137 | *
138 | * 中国联通
139 | * 中国移动
140 | * 中国电信
141 | *
142 | *
143 | * @param context 上下文
144 | * @return 移动网络运营商名称
145 | */
146 | fun getNetworkOperatorName(context: Context): String {
147 | val tm = context
148 | .getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
149 | return tm.networkOperatorName
150 | }
151 |
152 | /**
153 | * 获取移动终端类型
154 | *
155 | * @param context 上下文
156 | * @return 手机制式
157 | *
158 | * - {@link TelephonyManager#PHONE_TYPE_NONE } : 0 手机制式未知
159 | * - {@link TelephonyManager#PHONE_TYPE_GSM } : 1 手机制式为GSM,移动和联通
160 | * - {@link TelephonyManager#PHONE_TYPE_CDMA } : 2 手机制式为CDMA,电信
161 | * - {@link TelephonyManager#PHONE_TYPE_SIP } : 3
162 | *
163 | */
164 | fun getPhoneType(context: Context): Int {
165 | val tm = context
166 | .getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
167 | return tm.phoneType
168 | }
169 |
170 | /**
171 | * 判断是否Wifi连接
172 | *
173 | * @param context 上下文
174 | * @return true 如果是wifi连接
175 | */
176 | fun isWifiConnected(context: Context): Boolean {
177 | val activeInfo = getActiveNetworkInfo(context)
178 | return activeInfo?.run { isConnected && type == ConnectivityManager.TYPE_WIFI } ?: false
179 | }
180 |
181 | private class NetConnChangedReceiver : BroadcastReceiver() {
182 |
183 | override fun onReceive(context: Context, intent: Intent) {
184 | log("onReceive")
185 | val activeInfo = getActiveNetworkInfo(context)
186 | if (activeInfo == null) {
187 | broadcastConnStatus(ConnectStatus.NO_NETWORK)
188 | } else if (activeInfo!!.isConnected) {
189 | val networkType = activeInfo!!.type
190 | if (ConnectivityManager.TYPE_WIFI == networkType) {
191 | broadcastConnStatus(ConnectStatus.WIFI)
192 | } else if (ConnectivityManager.TYPE_MOBILE == networkType) {
193 | broadcastConnStatus(ConnectStatus.MOBILE)
194 | val subtype = activeInfo!!.subtype
195 | if (TelephonyManager.NETWORK_TYPE_GPRS == subtype
196 | || TelephonyManager.NETWORK_TYPE_GSM == subtype
197 | || TelephonyManager.NETWORK_TYPE_EDGE == subtype
198 | || TelephonyManager.NETWORK_TYPE_CDMA == subtype
199 | || TelephonyManager.NETWORK_TYPE_1xRTT == subtype
200 | || TelephonyManager.NETWORK_TYPE_IDEN == subtype
201 | ) {
202 | broadcastConnStatus(ConnectStatus.MOBILE_2G)
203 | } else if (TelephonyManager.NETWORK_TYPE_UMTS == subtype
204 | || TelephonyManager.NETWORK_TYPE_EVDO_0 == subtype
205 | || TelephonyManager.NETWORK_TYPE_EVDO_A == subtype
206 | || TelephonyManager.NETWORK_TYPE_HSDPA == subtype
207 | || TelephonyManager.NETWORK_TYPE_HSUPA == subtype
208 | || TelephonyManager.NETWORK_TYPE_HSPA == subtype
209 | || TelephonyManager.NETWORK_TYPE_EVDO_B == subtype
210 | || TelephonyManager.NETWORK_TYPE_EHRPD == subtype
211 | || TelephonyManager.NETWORK_TYPE_HSPAP == subtype
212 | || TelephonyManager.NETWORK_TYPE_TD_SCDMA == subtype
213 | ) {
214 | broadcastConnStatus(ConnectStatus.MOBILE_3G)
215 | } else if (TelephonyManager.NETWORK_TYPE_LTE == subtype || TelephonyManager.NETWORK_TYPE_IWLAN == subtype) {
216 | broadcastConnStatus(ConnectStatus.MOBILE_4G)
217 | } else {
218 | broadcastConnStatus(ConnectStatus.MOBILE_UNKNOWN)
219 | }
220 | } else {
221 | broadcastConnStatus(ConnectStatus.OTHER)
222 | }
223 | } else {
224 | broadcastConnStatus(ConnectStatus.NO_CONNECTED)
225 | }
226 | }
227 | }
228 |
229 | /**
230 | * 注册网络接收者
231 | * @param context 上下文
232 | */
233 | fun registerNetConnChangedReceiver(context: Context) {
234 | val filter = IntentFilter()
235 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
236 | context.registerReceiver(sNetConnChangedReceiver, filter)
237 | }
238 |
239 | /**
240 | * 取消注册网络接收者
241 | * * @param context 上下文
242 | */
243 | fun unregisterNetConnChangedReceiver(context: Context) {
244 | context.unregisterReceiver(sNetConnChangedReceiver)
245 | sNetConnChangedListeners.clear()
246 | }
247 |
248 | /**
249 | * 添加网络状态变化监听
250 | *
251 | * @param listener 网络连接状态改变监听
252 | */
253 | fun addNetConnChangedListener(listener: NetConnChangedListener) {
254 | val result = sNetConnChangedListeners.add(listener)
255 | log("addNetConnChangedListener: $result")
256 | }
257 |
258 | /**
259 | * 移除指定网络变化监听
260 | *
261 | * @param listener 网络连接状态改变监听
262 | */
263 | fun removeNetConnChangedListener(listener: NetConnChangedListener) {
264 | val result = sNetConnChangedListeners.remove(listener)
265 | log("removeNetConnChangedListener: $result")
266 | }
267 |
268 | private fun broadcastConnStatus(connectStatus: ConnectStatus) {
269 | val size = sNetConnChangedListeners.size
270 | if (size == 0) {
271 | return
272 | }
273 | for (i in 0 until size) {
274 | sNetConnChangedListeners[i].onNetConnChanged(connectStatus)
275 | }
276 | }
277 |
278 | private fun getActiveNetworkInfo(context: Context): NetworkInfo? {
279 | val connMgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
280 | return connMgr.activeNetworkInfo
281 | }
282 |
283 | private fun log(msg: String) {
284 | if (D) {
285 | Log.e(TAG, msg)
286 | }
287 | }
288 | }
289 | }
--------------------------------------------------------------------------------
/NetUtil.java:
--------------------------------------------------------------------------------
1 | package com.tw.sysnetdemo;
2 |
3 | import android.Manifest;
4 | import android.content.BroadcastReceiver;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.content.IntentFilter;
8 | import android.net.ConnectivityManager;
9 | import android.net.NetworkInfo;
10 | import android.telephony.TelephonyManager;
11 | import android.util.Log;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | /**
17 | * 网络工具类
18 | * 参考{@url https://developer.android.com/training/basics/network-ops/managing.html}
19 | * 需要权限:
20 | * {@link Manifest.permission#ACCESS_NETWORK_STATE}
21 | * ConnectivityManager: Answers queries about the state of network connectivity. It also notifies applications when network connectivity changes.
22 | * NetworkInfo: Describes the status of a network interface of a given type (currently either Mobile or Wi-Fi).
23 | */
24 | public final class NetUtil {
25 | private NetUtil() {
26 | throw new IllegalStateException("No instance!");
27 | }
28 |
29 | private static final String TAG = "NetUtil";
30 | private static final boolean D = true;
31 | private static NetConnChangedReceiver sNetConnChangedReceiver = new NetConnChangedReceiver();
32 | private static List sNetConnChangedListeners = new ArrayList<>();
33 |
34 | /**
35 | * 网络接口是否可用(即网络连接是否可行)和/或连接(即是否存在网络连接,是否可以建立套接字并传递数据)
36 | *
37 | * @param context 上下文
38 | * @return {@code true} 网络可用
39 | */
40 | public static boolean isNetConnected(Context context) {
41 | checkNonNull(context, "context == null");
42 | NetworkInfo activeInfo = getActiveNetworkInfo(context);
43 | return (activeInfo != null && activeInfo.isConnected());
44 | }
45 |
46 | /**
47 | * 是否移动数据连接
48 | *
49 | * @param context 上下文
50 | * @return {@code true} 移动数据连接
51 | */
52 | public static boolean isMobileConnected(Context context) {
53 | checkNonNull(context, "context == null");
54 | NetworkInfo activeInfo = getActiveNetworkInfo(context);
55 | return (activeInfo != null && activeInfo.isConnected() && activeInfo.getType() == ConnectivityManager.TYPE_MOBILE);
56 | }
57 |
58 | /**
59 | * 是否2G网络连接
60 | *
61 | * @param context 上下文
62 | * @return {@code true} 2G网络连接
63 | */
64 | public static boolean is2GConnected(Context context) {
65 | checkNonNull(context, "context == null");
66 | NetworkInfo activeInfo = getActiveNetworkInfo(context);
67 | if (activeInfo == null || !activeInfo.isConnected()) {
68 | return false;
69 | }
70 | int subtype = activeInfo.getSubtype();
71 | switch (subtype) {
72 | case TelephonyManager.NETWORK_TYPE_GPRS:
73 | case TelephonyManager.NETWORK_TYPE_GSM:
74 | case TelephonyManager.NETWORK_TYPE_EDGE:
75 | case TelephonyManager.NETWORK_TYPE_CDMA:
76 | case TelephonyManager.NETWORK_TYPE_1xRTT:
77 | case TelephonyManager.NETWORK_TYPE_IDEN:
78 | return true;
79 | default:
80 | return false;
81 | }
82 | }
83 |
84 | /**
85 | * 是否3G网络连接
86 | *
87 | * @param context 上下文
88 | * @return {@code true} 3G网络连接
89 | */
90 | public static boolean is3GConnected(Context context) {
91 | checkNonNull(context, "context == null");
92 | NetworkInfo activeInfo = getActiveNetworkInfo(context);
93 | if (activeInfo == null || !activeInfo.isConnected()) {
94 | return false;
95 | }
96 | int subtype = activeInfo.getSubtype();
97 | switch (subtype) {
98 | case TelephonyManager.NETWORK_TYPE_UMTS:
99 | case TelephonyManager.NETWORK_TYPE_EVDO_0:
100 | case TelephonyManager.NETWORK_TYPE_EVDO_A:
101 | case TelephonyManager.NETWORK_TYPE_HSDPA:
102 | case TelephonyManager.NETWORK_TYPE_HSUPA:
103 | case TelephonyManager.NETWORK_TYPE_HSPA:
104 | case TelephonyManager.NETWORK_TYPE_EVDO_B:
105 | case TelephonyManager.NETWORK_TYPE_EHRPD:
106 | case TelephonyManager.NETWORK_TYPE_HSPAP:
107 | case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
108 | return true;
109 | default:
110 | return false;
111 | }
112 | }
113 |
114 | /**
115 | * 是否4G网络连接
116 | *
117 | * @param context 上下文
118 | * @return {@code true} 4G网络连接
119 | */
120 | public static boolean is4GConnected(Context context) {
121 | checkNonNull(context, "context == null");
122 | NetworkInfo activeInfo = getActiveNetworkInfo(context);
123 | if (activeInfo == null || !activeInfo.isConnected()) {
124 | return false;
125 | }
126 | int subtype = activeInfo.getSubtype();
127 | switch (subtype) {
128 | case TelephonyManager.NETWORK_TYPE_LTE:
129 | case TelephonyManager.NETWORK_TYPE_IWLAN:
130 | return true;
131 | default:
132 | return false;
133 | }
134 | }
135 |
136 | /**
137 | * 获取移动网络运营商名称
138 | *
139 | * 中国联通
140 | * 中国移动
141 | * 中国电信
142 | *
143 | *
144 | * @param context 上下文
145 | * @return 移动网络运营商名称
146 | */
147 | public static String getNetworkOperatorName(Context context) {
148 | checkNonNull(context, "context == null");
149 | TelephonyManager tm = (TelephonyManager) context
150 | .getSystemService(Context.TELEPHONY_SERVICE);
151 | return tm.getNetworkOperatorName();
152 | }
153 |
154 | /**
155 | * 获取移动终端类型
156 | *
157 | * @param context 上下文
158 | * @return 手机制式
159 | *
160 | * - {@link TelephonyManager#PHONE_TYPE_NONE } : 0 手机制式未知
161 | * - {@link TelephonyManager#PHONE_TYPE_GSM } : 1 手机制式为GSM,移动和联通
162 | * - {@link TelephonyManager#PHONE_TYPE_CDMA } : 2 手机制式为CDMA,电信
163 | * - {@link TelephonyManager#PHONE_TYPE_SIP } : 3
164 | *
165 | */
166 | public static int getPhoneType(Context context) {
167 | checkNonNull(context, "context == null");
168 | TelephonyManager tm = (TelephonyManager) context
169 | .getSystemService(Context.TELEPHONY_SERVICE);
170 | return tm.getPhoneType();
171 | }
172 |
173 | /**
174 | * 判断是否Wifi连接
175 | *
176 | * @param context 上下文
177 | * @return true 如果是wifi连接
178 | */
179 | public static boolean isWifiConnected(Context context) {
180 | checkNonNull(context, "context == null");
181 | NetworkInfo activeInfo = getActiveNetworkInfo(context);
182 | return (activeInfo != null && activeInfo.isConnected() && activeInfo.getType() == ConnectivityManager.TYPE_WIFI);
183 | }
184 |
185 |
186 | private static final class NetConnChangedReceiver extends BroadcastReceiver {
187 |
188 | @Override
189 | public void onReceive(Context context, Intent intent) {
190 | log("onReceive");
191 | NetworkInfo activeInfo = getActiveNetworkInfo(context);
192 | if (activeInfo == null) {
193 | broadcastConnStatus(ConnectStatus.NO_NETWORK);
194 | } else if (activeInfo.isConnected()) {
195 | int networkType = activeInfo.getType();
196 | if (ConnectivityManager.TYPE_WIFI == networkType) {
197 | broadcastConnStatus(ConnectStatus.WIFI);
198 | } else if (ConnectivityManager.TYPE_MOBILE == networkType) {
199 | broadcastConnStatus(ConnectStatus.MOBILE);
200 | int subtype = activeInfo.getSubtype();
201 | if (TelephonyManager.NETWORK_TYPE_GPRS == subtype
202 | || TelephonyManager.NETWORK_TYPE_GSM == subtype
203 | || TelephonyManager.NETWORK_TYPE_EDGE == subtype
204 | || TelephonyManager.NETWORK_TYPE_CDMA == subtype
205 | || TelephonyManager.NETWORK_TYPE_1xRTT == subtype
206 | || TelephonyManager.NETWORK_TYPE_IDEN == subtype) {
207 | broadcastConnStatus(ConnectStatus.MOBILE_2G);
208 | } else if (TelephonyManager.NETWORK_TYPE_UMTS == subtype
209 | || TelephonyManager.NETWORK_TYPE_EVDO_0 == subtype
210 | || TelephonyManager.NETWORK_TYPE_EVDO_A == subtype
211 | || TelephonyManager.NETWORK_TYPE_HSDPA == subtype
212 | || TelephonyManager.NETWORK_TYPE_HSUPA == subtype
213 | || TelephonyManager.NETWORK_TYPE_HSPA == subtype
214 | || TelephonyManager.NETWORK_TYPE_EVDO_B == subtype
215 | || TelephonyManager.NETWORK_TYPE_EHRPD == subtype
216 | || TelephonyManager.NETWORK_TYPE_HSPAP == subtype
217 | || TelephonyManager.NETWORK_TYPE_TD_SCDMA == subtype) {
218 | broadcastConnStatus(ConnectStatus.MOBILE_3G);
219 | } else if (TelephonyManager.NETWORK_TYPE_LTE == subtype
220 | || TelephonyManager.NETWORK_TYPE_IWLAN == subtype) {
221 | broadcastConnStatus(ConnectStatus.MOBILE_4G);
222 | } else {
223 | broadcastConnStatus(ConnectStatus.MOBILE_UNKNOWN);
224 | }
225 | } else {
226 | broadcastConnStatus(ConnectStatus.OTHER);
227 | }
228 | } else {
229 | broadcastConnStatus(ConnectStatus.NO_CONNECTED);
230 | }
231 | }
232 | }
233 |
234 | /**
235 | * 注册网络接收者
236 | * * @param context 上下文
237 | */
238 | public static void registerNetConnChangedReceiver(Context context) {
239 | checkNonNull(context, "context == null");
240 | IntentFilter filter = new IntentFilter();
241 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
242 | context.registerReceiver(sNetConnChangedReceiver, filter);
243 | }
244 |
245 | /**
246 | * 取消注册网络接收者
247 | * * @param context 上下文
248 | */
249 | public static void unregisterNetConnChangedReceiver(Context context) {
250 | checkNonNull(context, "context == null");
251 | context.unregisterReceiver(sNetConnChangedReceiver);
252 | sNetConnChangedListeners.clear();
253 | }
254 |
255 | /**
256 | * 添加网络状态变化监听
257 | *
258 | * @param listener 网络连接状态改变监听
259 | */
260 | public static void addNetConnChangedListener(NetConnChangedListener listener) {
261 | checkNonNull(listener, "listener == null");
262 | boolean result = sNetConnChangedListeners.add(listener);
263 | log("addNetConnChangedListener: " + result);
264 | }
265 |
266 | /**
267 | * 移除指定网络变化监听
268 | *
269 | * @param listener 网络连接状态改变监听
270 | */
271 | public static void removeNetConnChangedListener(NetConnChangedListener listener) {
272 | checkNonNull(listener, "listener == null");
273 | boolean result = sNetConnChangedListeners.remove(listener);
274 | log("removeNetConnChangedListener: " + result);
275 | }
276 |
277 | private static void broadcastConnStatus(ConnectStatus connectStatus) {
278 | int size = sNetConnChangedListeners.size();
279 | if (size == 0) {
280 | return;
281 | }
282 | for (int i = 0; i < size; i++) {
283 | sNetConnChangedListeners.get(i).onNetConnChanged(connectStatus);
284 | }
285 | }
286 |
287 | private static NetworkInfo getActiveNetworkInfo(Context context) {
288 | ConnectivityManager connMgr = (ConnectivityManager)
289 | context.getSystemService(Context.CONNECTIVITY_SERVICE);
290 | return connMgr.getActiveNetworkInfo();
291 | }
292 |
293 | private static void checkNonNull(Object object, String message) {
294 | if (object == null) {
295 | throw new IllegalArgumentException(message);
296 | }
297 | }
298 |
299 | private static void log(String msg) {
300 | if (D)
301 | Log.e(TAG, msg);
302 | }
303 |
304 | public interface NetConnChangedListener {
305 | void onNetConnChanged(ConnectStatus connectStatus);
306 | }
307 |
308 | public enum ConnectStatus {
309 | NO_NETWORK,
310 | WIFI,
311 | MOBILE,
312 | MOBILE_2G,
313 | MOBILE_3G,
314 | MOBILE_4G,
315 | MOBILE_UNKNOWN,
316 | OTHER,
317 | NO_CONNECTED
318 | }
319 | }
320 |
--------------------------------------------------------------------------------