標籤:
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 context28 * 上下文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 kbps60 case TelephonyManager.NETWORK_TYPE_CDMA:61 return false; // ~ 14-64 kbps62 case TelephonyManager.NETWORK_TYPE_EDGE:63 return false; // ~ 50-100 kbps64 case TelephonyManager.NETWORK_TYPE_EVDO_0:65 return true; // ~ 400-1000 kbps66 case TelephonyManager.NETWORK_TYPE_EVDO_A:67 return true; // ~ 600-1400 kbps68 case TelephonyManager.NETWORK_TYPE_GPRS:69 return false; // ~ 100 kbps70 case TelephonyManager.NETWORK_TYPE_HSDPA:71 return true; // ~ 2-14 Mbps72 case TelephonyManager.NETWORK_TYPE_HSPA:73 return true; // ~ 700-1700 kbps74 case TelephonyManager.NETWORK_TYPE_HSUPA:75 return true; // ~ 1-23 Mbps76 case TelephonyManager.NETWORK_TYPE_UMTS:77 return true; // ~ 400-7000 kbps78 case TelephonyManager.NETWORK_TYPE_EHRPD:79 return true; // ~ 1-2 Mbps80 case TelephonyManager.NETWORK_TYPE_EVDO_B:81 return true; // ~ 5 Mbps82 case TelephonyManager.NETWORK_TYPE_HSPAP:83 return true; // ~ 10-20 Mbps84 case TelephonyManager.NETWORK_TYPE_IDEN:85 return false; // ~25 kbps86 case TelephonyManager.NETWORK_TYPE_LTE:87 return true; // ~ 10+ Mbps88 case TelephonyManager.NETWORK_TYPE_UNKNOWN:89 return false;90 default:91 return false;92 }93 }94 }
android開發擷取網路狀態,wifi,wap,2g,3g.工具類(一)