下面这个方法用于获取系统当前网络类型:
1: public static int getNetworkType(Context context) {
2: ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
3:
4: if (connectivity != null) {
5: // 获取网络连接管理的对象
6: NetworkInfo info = connectivity.getActiveNetworkInfo();
7:
8: if (info != null && info.isConnected()) {
9: // 判断当前网络是否已经连接
10: if (info.getState() == NetworkInfo.State.CONNECTED) {
11: if (info.getTypeName().equals("WIFI")) {
12: return MrDefines.NETTYPE_WIFI;
13: } else {
14: Cursor mCursor = context.getContentResolver().query(Uri.parse("content://telephony/carriers"),
15: new String[] {"name"}, "current=1", null, null);
16:
17: if(mCursor != null && mCursor.moveToFirst()){
18: String name = mCursor.getString(0);
19:
20: if(name.equalsIgnoreCase("cmnet"))
21: return MrDefines.NETTYPE_CMNET;
22: else if(name.equalsIgnoreCase("cmwap"))
23: return MrDefines.NETTYPE_CMWAP;
24: }
25: }
26: }
27: }
28: }
29:
30: return MrDefines.NETTYPE_UNKNOW;
31: }
1: public static final int NETTYPE_WIFI=0,
2: NETTYPE_CMWAP=1,
3: NETTYPE_CMNET=2,
4: NETTYPE_UNKNOW=3;