本文实例讲述了java获取网络类型的方法。分享给大家供大家参考。具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
/**** * 获取网络类型 * * @param context * @return */ public static String getNetType(Context context) { try { ConnectivityManager connectMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = connectMgr.getActiveNetworkInfo(); if (info == null ) { return "" ; } if (info.getType() == ConnectivityManager.TYPE_WIFI) { return "WIFI" ; } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) { if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA) { return "CDMA" ; } else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) { return "EDGE" ; } else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_0) { return "EVDO0" ; } else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_A) { return "EVDOA" ; } else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS) { return "GPRS" ; } /* * else if(info.getSubtype() == * TelephonyManager.NETWORK_TYPE_HSDPA){ return "HSDPA"; }else * if(info.getSubtype() == TelephonyManager.NETWORK_TYPE_HSPA){ * return "HSPA"; }else if(info.getSubtype() == * TelephonyManager.NETWORK_TYPE_HSUPA){ return "HSUPA"; } */ else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_UMTS) { return "UMTS" ; } else { return "3G" ; } } else { return "" ; } } catch (Exception e) { return "" ; } } |
希望本文所述对大家的java程序设计有所帮助。