Android獲取網絡

// 獲取網絡類型
    public static String getNetworkType(Context context) {
        String typeString = "UNKNOW";
        NetworkInfo networkInfo = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                typeString = "WIFI";
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                int networkType = networkInfo.getSubtype();
                switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                        typeString = "2G";
                        break;
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                        typeString = "3G";
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        typeString = "4G";
                        break;
                    default:
                        String typeName = networkInfo.getSubtypeName();
                        if (typeName.equalsIgnoreCase("TD-SCDMA")
                                || typeName.equalsIgnoreCase("WCDMA")
                                || typeName.equalsIgnoreCase("CDMA2000")) {
                            typeString = "3G";
                        } else {
                            typeString = typeName;
                        }
                        break;
                }
            }
        }
        return typeString;
    }
相關文章
相關標籤/搜索