android开发获取网络状态,wifi,wap,2g,3g.工具类(一)

android开发获取网络状态整理:

 1 package com.gzcivil.utils;
 2 
 3 import android.content.Context;
 4 import android.net.ConnectivityManager;
 5 import android.net.NetworkInfo;
 6 import android.telephony.TelephonyManager;
 7 import android.text.TextUtils;
 8 
 9 public class NetworkUtils {
10 
11     /** 没有网络 */
12     public static final int NETWORKTYPE_INVALID = 0;
13     /** wap网络 */
14     public static final int NETWORKTYPE_WAP = 1;
15     /** 2G网络 */
16     public static final int NETWORKTYPE_2G = 2;
17     /** 3G和3G以上网络,或统称为快速网络 */
18     public static final int NETWORKTYPE_3G = 3;
19     /** USB网络 */
20     public static final int NETWORKTYPE_USB = 4;
21     /** wifi网络 */
22     public static final int NETWORKTYPE_WIFI = 5;
23 
24     /**
25      * 获取网络状态,wifi,wap,2g,3g.
26      * 
27      * @param context
28      *            上下文
29      * @return int 网络状态 {@link #NETWORKTYPE_2G},{@link #NETWORKTYPE_3G}, *
30      *         {@link #NETWORKTYPE_INVALID},{@link #NETWORKTYPE_WAP}*
31      *         <p>
32      *         {@link #NETWORKTYPE_WIFI}
33      */
34     public static int getNetWorkType(Context context) {
35         ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
36         NetworkInfo networkInfo = manager.getActiveNetworkInfo();
37         int mNetWorkType = 0;
38         if (networkInfo != null && networkInfo.isConnected()) {
39             String type = networkInfo.getTypeName();
40             if (type.equalsIgnoreCase("WIFI")) {
41                 mNetWorkType = NETWORKTYPE_WIFI;
42             } else if (type.equalsIgnoreCase("usb")) {
43                 mNetWorkType = NETWORKTYPE_USB;
44             } else if (type.equalsIgnoreCase("MOBILE")) {
45                 @SuppressWarnings("deprecation")
46                 String proxyHost = android.net.Proxy.getDefaultHost();
47                 mNetWorkType = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) : NETWORKTYPE_WAP;
48             }
49         } else {
50             mNetWorkType = NETWORKTYPE_INVALID;
51         }
52         return mNetWorkType;
53     }
54     
55     private static boolean isFastMobileNetwork(Context context) {
56         TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
57         switch (telephonyManager.getNetworkType()) {
58         case TelephonyManager.NETWORK_TYPE_1xRTT:
59             return false; // ~ 50-100 kbps
60         case TelephonyManager.NETWORK_TYPE_CDMA:
61             return false; // ~ 14-64 kbps
62         case TelephonyManager.NETWORK_TYPE_EDGE:
63             return false; // ~ 50-100 kbps
64         case TelephonyManager.NETWORK_TYPE_EVDO_0:
65             return true; // ~ 400-1000 kbps
66         case TelephonyManager.NETWORK_TYPE_EVDO_A:
67             return true; // ~ 600-1400 kbps
68         case TelephonyManager.NETWORK_TYPE_GPRS:
69             return false; // ~ 100 kbps
70         case TelephonyManager.NETWORK_TYPE_HSDPA:
71             return true; // ~ 2-14 Mbps
72         case TelephonyManager.NETWORK_TYPE_HSPA:
73             return true; // ~ 700-1700 kbps
74         case TelephonyManager.NETWORK_TYPE_HSUPA:
75             return true; // ~ 1-23 Mbps
76         case TelephonyManager.NETWORK_TYPE_UMTS:
77             return true; // ~ 400-7000 kbps
78         case TelephonyManager.NETWORK_TYPE_EHRPD:
79             return true; // ~ 1-2 Mbps
80         case TelephonyManager.NETWORK_TYPE_EVDO_B:
81             return true; // ~ 5 Mbps
82         case TelephonyManager.NETWORK_TYPE_HSPAP:
83             return true; // ~ 10-20 Mbps
84         case TelephonyManager.NETWORK_TYPE_IDEN:
85             return false; // ~25 kbps
86         case TelephonyManager.NETWORK_TYPE_LTE:
87             return true; // ~ 10+ Mbps
88         case TelephonyManager.NETWORK_TYPE_UNKNOWN:
89             return false;
90         default:
91             return false;
92         }
93     }
94 }

 

你可能感兴趣的:(android开发获取网络状态,wifi,wap,2g,3g.工具类(一))