Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.telephony.TelephonyManager; import android.text.TextUtils; public class Main { public static final String NETWORK_TYPE_2G = "2g"; public static final String NETWORK_TYPE_3G = "eg"; public static final String NETWORK_TYPE_DISCONNECT = "disconnect"; public static final String NETWORK_TYPE_UNKNOWN = "unknown"; public static final String NETWORK_TYPE_WAP = "wap"; public static final String NETWORK_TYPE_WIFI = "wifi"; public static String getNetworkTypeName(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; String type = NETWORK_TYPE_DISCONNECT; if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) { return type; } if (networkInfo.isConnected()) { String typeName = networkInfo.getTypeName(); if ("WIFI".equalsIgnoreCase(typeName)) { type = NETWORK_TYPE_WIFI; } else if ("MOBILE".equalsIgnoreCase(typeName)) { String proxyHost = android.net.Proxy.getDefaultHost(); type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G) : NETWORK_TYPE_WAP; } else { type = NETWORK_TYPE_UNKNOWN; } } return type; } /** * Whether is fast mobile network * * @param context * @return */ private static boolean isFastMobileNetwork(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager == null) { return false; } switch (telephonyManager.getNetworkType()) { case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_HSPAP: case TelephonyManager.NETWORK_TYPE_LTE: return true; case TelephonyManager.NETWORK_TYPE_IDEN: case TelephonyManager.NETWORK_TYPE_UNKNOWN: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_GPRS: default: return false; } } /** * Get network type * * @param context * @return */ public static int getNetworkType(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo(); return networkInfo == null ? -1 : networkInfo.getType(); } }