Android系统可以通过WIFI和移动网络来连接互联网,系统网络连接状态是通过ConnectivityManager 类来获取的。关键代码如下:
[java] view plain copy
1. ConnectivityManager connManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
对于网络状态的判断,Android提供了大量的API,我们只要善于利用这些API就能很很容易获取各种网络的状态信息。总结的各种网络状态接口如下:
[java] view plain copy
1. // API都有只不过是将其进行综合并作为自己工程中的工具函数使用,很方便的。
2. /**
3. * 获取当前网络状态的类型 *
4. *
5. * @param mContext
6. * @return 返回网络类型
7. */
8. public static final int NETWORK_TYPE_NONE = -0x1; // 断网情况
9. public static final int NETWORK_TYPE_WIFI = 0x1; // WIFI模式
10. public static final int NETWOKR_TYPE_MOBILE = 0x2; // GPRS模式
11.
12. public static int getCurrentNetType(Context mContext) {
13. ConnectivityManager connManager = (ConnectivityManager) mContext
14. .getSystemService(Context.CONNECTIVITY_SERVICE);
15. NetworkInfo wifi = connManager
16. // WIFI
17. NetworkInfo gprs = connManager
18. // GPRS
19.
20. if (wifi != null && wifi.getState() == State.CONNECTED) {
21. "Current net type: WIFI.");
22. return NETWORK_TYPE_WIFI;
23. else if (gprs != null && gprs.getState() == State.CONNECTED) {
24. "Current net type: GPRS.");
25. return NETWOKR_TYPE_MOBILE;
26. }
27. "Current net type: NONE.");
28. return NETWORK_TYPE_NONE;
29. }
30.
31. /**
32. * 判断Android客户端网络是否连接
33. * 只能判断是否有可用的连接,而不能判断是否能连网
34. * @param context
35. * @return true/false
36. */
37. public static boolean checkNet(Context context) {
38. try {
39. ConnectivityManager connectivity = (ConnectivityManager) context
40. .getSystemService(Context.CONNECTIVITY_SERVICE);
41. if (connectivity != null) {
42. NetworkInfo info = connectivity.getActiveNetworkInfo();
43. if (info != null && info.isConnected()) {
44. if (info.getState() == NetworkInfo.State.CONNECTED) {
45. return true;
46. }
47. }
48. }
49. catch (Exception e) {
50. return false;
51. }
52. return false;
53. }
54.
55. /**
56. * 检验网络连接 并toast提示
57. * @return
58. */
59. public boolean noteIntentConnect(Context context) {
60. ConnectivityManager con = (ConnectivityManager) context
61. .getSystemService(Context.CONNECTIVITY_SERVICE);
62. NetworkInfo networkinfo = con.getActiveNetworkInfo();
63. if (networkinfo == null || !networkinfo.isAvailable()) {
64. // 当前网络不可用
65. "请先连接Internet!",
66. Toast.LENGTH_SHORT).show();
67. return false;
68. }
69. boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
70. .isConnectedOrConnecting();
71. if (!wifi) { // 提示使用WIFI
72. "建议您使用WIFI以减少流量!",
73. Toast.LENGTH_SHORT).show();
74. }
75. return true;
76.
77. }
78.
79. /**
80. * 判断网络连接是否可用
81. *
82. * @param context
83. * @return
84. */
85. public static boolean isNetworkAvailable(Context context) {
86. ConnectivityManager cm = (ConnectivityManager) context
87. .getSystemService(Context.CONNECTIVITY_SERVICE);
88. if (cm == null) {
89. else {
90. // 如果仅仅是用来判断网络连接
91. // 则可以使用 cm.getActiveNetworkInfo().isAvailable();
92. NetworkInfo[] info = cm.getAllNetworkInfo();
93. if (info != null) {
94. for (int i = 0; i < info.length; i++) {
95. if (info[i].getState() == NetworkInfo.State.CONNECTED) {
96. return true;
97. }
98. }
99. }
100. }
101. return false;
102. }
103.
104. /**
105. * 判断网络状态是否可用
106. * @return true: 网络可用 ; false: 网络不可用
107. */
108. public boolean isConnectInternet(Context mContext) {
109. ConnectivityManager conManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
110. NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
111. if (networkInfo != null) { // 注意,这个判断一定要的,要不然会出错
112. return networkInfo.isAvailable();
113. }
114. return false;
115. }
116.
117. /**
118. * 判断GPS是否打开
119. *
120. * @param context
121. * @return
122. */
123. public static boolean isGpsEnabled(Context context) {
124. LocationManager lm = ((LocationManager) context
125. .getSystemService(Context.LOCATION_SERVICE));
126. true);
127. return accessibleProviders != null && accessibleProviders.size() > 0;
128. }
129.
130. /**
131. * 判断WIFI是否打开
132. *
133. * @param context
134. * @return
135. */
136. public static boolean isWifiEnabled(Context context) {
137. ConnectivityManager mgrConn = (ConnectivityManager) context
138. .getSystemService(Context.CONNECTIVITY_SERVICE);
139. TelephonyManager mgrTel = (TelephonyManager) context
140. .getSystemService(Context.TELEPHONY_SERVICE);
141. return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
142. .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
143. .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
144. }
145.
146. /**
147. * 判断是否是3G网络
148. *
149. * @param context
150. * @return
151. */
152. public static boolean is3gNet(Context context) {
153. ConnectivityManager cm = (ConnectivityManager) context
154. .getSystemService(Context.CONNECTIVITY_SERVICE);
155. NetworkInfo networkINfo = cm.getActiveNetworkInfo();
156. if (networkINfo != null
157. && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {
158. return true;
159. }
160. return false;
161. }
162.
163. /**
164. * 判断是否是WIFI网络
165. *
166. * @param context
167. * @return
168. */
169. public static boolean isWifiNet(Context context) {
170. ConnectivityManager cm = (ConnectivityManager) context
171. .getSystemService(Context.CONNECTIVITY_SERVICE);
172. NetworkInfo networkINfo = cm.getActiveNetworkInfo();
173. if (networkINfo != null
174. && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
175. return true;
176. }
177. return false;
178. }
Android系统可以通过WIFI和移动网络来连接互联网,系统网络连接状态是通过ConnectivityManager 类来获取的。关键代码如下:
[java] view plain copy
1. ConnectivityManager connManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
对于网络状态的判断,Android提供了大量的API,我们只要善于利用这些API就能很很容易获取各种网络的状态信息。总结的各种网络状态接口如下:
[java] view plain copy
1. // API都有只不过是将其进行综合并作为自己工程中的工具函数使用,很方便的。
2. /**
3. * 获取当前网络状态的类型 *
4. *
5. * @param mContext
6. * @return 返回网络类型
7. */
8. public static final int NETWORK_TYPE_NONE = -0x1; // 断网情况
9. public static final int NETWORK_TYPE_WIFI = 0x1; // WIFI模式
10. public static final int NETWOKR_TYPE_MOBILE = 0x2; // GPRS模式
11.
12. public static int getCurrentNetType(Context mContext) {
13. ConnectivityManager connManager = (ConnectivityManager) mContext
14. .getSystemService(Context.CONNECTIVITY_SERVICE);
15. NetworkInfo wifi = connManager
16. // WIFI
17. NetworkInfo gprs = connManager
18. // GPRS
19.
20. if (wifi != null && wifi.getState() == State.CONNECTED) {
21. "Current net type: WIFI.");
22. return NETWORK_TYPE_WIFI;
23. else if (gprs != null && gprs.getState() == State.CONNECTED) {
24. "Current net type: GPRS.");
25. return NETWOKR_TYPE_MOBILE;
26. }
27. "Current net type: NONE.");
28. return NETWORK_TYPE_NONE;
29. }
30.
31. /**
32. * 判断Android客户端网络是否连接
33. * 只能判断是否有可用的连接,而不能判断是否能连网
34. * @param context
35. * @return true/false
36. */
37. public static boolean checkNet(Context context) {
38. try {
39. ConnectivityManager connectivity = (ConnectivityManager) context
40. .getSystemService(Context.CONNECTIVITY_SERVICE);
41. if (connectivity != null) {
42. NetworkInfo info = connectivity.getActiveNetworkInfo();
43. if (info != null && info.isConnected()) {
44. if (info.getState() == NetworkInfo.State.CONNECTED) {
45. return true;
46. }
47. }
48. }
49. catch (Exception e) {
50. return false;
51. }
52. return false;
53. }
54.
55. /**
56. * 检验网络连接 并toast提示
57. * @return
58. */
59. public boolean noteIntentConnect(Context context) {
60. ConnectivityManager con = (ConnectivityManager) context
61. .getSystemService(Context.CONNECTIVITY_SERVICE);
62. NetworkInfo networkinfo = con.getActiveNetworkInfo();
63. if (networkinfo == null || !networkinfo.isAvailable()) {
64. // 当前网络不可用
65. "请先连接Internet!",
66. Toast.LENGTH_SHORT).show();
67. return false;
68. }
69. boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
70. .isConnectedOrConnecting();
71. if (!wifi) { // 提示使用WIFI
72. "建议您使用WIFI以减少流量!",
73. Toast.LENGTH_SHORT).show();
74. }
75. return true;
76.
77. }
78.
79. /**
80. * 判断网络连接是否可用
81. *
82. * @param context
83. * @return
84. */
85. public static boolean isNetworkAvailable(Context context) {
86. ConnectivityManager cm = (ConnectivityManager) context
87. .getSystemService(Context.CONNECTIVITY_SERVICE);
88. if (cm == null) {
89. else {
90. // 如果仅仅是用来判断网络连接
91. // 则可以使用 cm.getActiveNetworkInfo().isAvailable();
92. NetworkInfo[] info = cm.getAllNetworkInfo();
93. if (info != null) {
94. for (int i = 0; i < info.length; i++) {
95. if (info[i].getState() == NetworkInfo.State.CONNECTED) {
96. return true;
97. }
98. }
99. }
100. }
101. return false;
102. }
103.
104. /**
105. * 判断网络状态是否可用
106. * @return true: 网络可用 ; false: 网络不可用
107. */
108. public boolean isConnectInternet(Context mContext) {
109. ConnectivityManager conManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
110. NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
111. if (networkInfo != null) { // 注意,这个判断一定要的,要不然会出错
112. return networkInfo.isAvailable();
113. }
114. return false;
115. }
116.
117. /**
118. * 判断GPS是否打开
119. *
120. * @param context
121. * @return
122. */
123. public static boolean isGpsEnabled(Context context) {
124. LocationManager lm = ((LocationManager) context
125. .getSystemService(Context.LOCATION_SERVICE));
126. true);
127. return accessibleProviders != null && accessibleProviders.size() > 0;
128. }
129.
130. /**
131. * 判断WIFI是否打开
132. *
133. * @param context
134. * @return
135. */
136. public static boolean isWifiEnabled(Context context) {
137. ConnectivityManager mgrConn = (ConnectivityManager) context
138. .getSystemService(Context.CONNECTIVITY_SERVICE);
139. TelephonyManager mgrTel = (TelephonyManager) context
140. .getSystemService(Context.TELEPHONY_SERVICE);
141. return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
142. .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
143. .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
144. }
145.
146. /**
147. * 判断是否是3G网络
148. *
149. * @param context
150. * @return
151. */
152. public static boolean is3gNet(Context context) {
153. ConnectivityManager cm = (ConnectivityManager) context
154. .getSystemService(Context.CONNECTIVITY_SERVICE);
155. NetworkInfo networkINfo = cm.getActiveNetworkInfo();
156. if (networkINfo != null
157. && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {
158. return true;
159. }
160. return false;
161. }
162.
163. /**
164. * 判断是否是WIFI网络
165. *
166. * @param context
167. * @return
168. */
169. public static boolean isWifiNet(Context context) {
170. ConnectivityManager cm = (ConnectivityManager) context
171. .getSystemService(Context.CONNECTIVITY_SERVICE);
172. NetworkInfo networkINfo = cm.getActiveNetworkInfo();
173. if (networkINfo != null
174. && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
175. return true;
176. }
177. return false;
178. }