When making Android Apps, in order to save users' traffic, to avoid arousing users' anger, and to better user experience, some adjustments need to be made based on the user's current network situation. You can also let the user choose whether to allow requests for some data with relatively large traffic under the 2G/3G/4G network conditions.
The NetworksInfo object can be obtained through the TelephonyManager and ConnectivityManager provided by Android. You can get the type through getType() to determine whether it is wifi or mobile. If it is mobile, you can get the network type and name through getSubType() and getSubTypeName() of the NetworksInfo object.
The network type and name are defined in the TelephonyManager class.
/** Network type is unknown */ public static final int NETWORK_TYPE_UNKNOWN = 0; /** Current network is GPRS */ public static final int NETWORK_TYPE_GPRS = 1; /** Current network is EDGE */ public static final int NETWORK_TYPE_EDGE = 2; /** Current network is UMTS */ public static final int NETWORK_TYPE_UMTS = 3; /** Current network is CDMA: Either IS95A or IS95B*/ public static final int NETWORK_TYPE_CDMA = 4; /** Current network is EVDO revision 0*/ public static final int NETWORK_TYPE_EVDO_0 = 5; /** Current network is EVDO revision A*/ public static final int NETWORK_TYPE_EVDO_A = 6; /** Current network is 1xRTT*/ public static final int NETWORK_TYPE_1xRTT = 7; /** Current network is HSDPA */ public static final int NETWORK_TYPE_HSDPA = 8; /** Current network is HSUPA */ public static final int NETWORK_TYPE_HSUPA = 9; /** Current network is HSPA */ public static final int NETWORK_TYPE_HSPA = 10; /** Current network is iDen */ public static final int NETWORK_TYPE_IDEN = 11; /** Current network is EVDO revision B*/ public static final int NETWORK_TYPE_EVDO_B = 12; /** Current network is LTE */ public static final int NETWORK_TYPE_LTE = 13; /** Current network is eHRPD */ public static final int NETWORK_TYPE_EHRPD = 14; /** Current network is HSPA+ */ public static final int NETWORK_TYPE_HSPAP = 15;
After seeing this code and comments, I believe that it will be difficult for people without this knowledge to understand. What are they? What is the difference between this comment and no comment? ! It just makes people feel more upset. So, comments are important to people who read the code. Of course, these things may be too professional. The person who writes these codes probably wants to write them but doesn’t know what’s wrong. How big does it have to be? ! At the end, I will post some information I have compiled for your reference. It is not very detailed or professional, but I probably have an impression.
TelephonyManager also provides the method of getNetworkTypeName(int type) , which can return a string, but the amount of information is not large.
So how do you determine whether it is a 2G, 3G or 4G network? TelephonyManager also provides another method, getNetworkClass(int networkType), but this method is hidden, so I'll post the code.
public static int getNetworkClass(int networkType) { switch (networkType) { case NETWORK_TYPE_GPRS: case NETWORK_TYPE_EDGE: case NETWORK_TYPE_CDMA: case NETWORK_TYPE_1xRTT: case NETWORK_TYPE_IDEN: return NETWORK_CLASS_2_G; case NETWORK_TYPE_UMTS: case NETWORK_TYPE_EVDO_0: case NETWORK_TYPE_EVDO_A: case NETWORK_TYPE_HSDPA: case NETWORK_TYPE_HSUPA: case NETWORK_TYPE_HSPA: case NETWORK_TYPE_EVDO_B: case NETWORK_TYPE_EHRPD: case NETWORK_TYPE_HSPAP: return NETWORK_CLASS_3_G; case NETWORK_TYPE_LTE: return NETWORK_CLASS_4_G; default: return NETWORK_CLASS_UNKNOWN; } }
Then the following are the values of these constants.
/** Unknown network class. {@hide} */ public static final int NETWORK_CLASS_UNKNOWN = 0; /** Class of broadly defined "2G" networks. {@hide} */ public static final int NETWORK_CLASS_2_G = 1; /** Class of broadly defined "3G" networks. {@hide} */ public static final int NETWORK_CLASS_3_G = 2; /** Class of broadly defined "4G" networks. {@hide} */ public static final int NETWORK_CLASS_4_G = 3;
I don’t know why I want to hide these things, it’s unreliable? Or is another better way? ! I don’t know, let’s do this first. Now through the above methods, you can know what network the user is using. Of course, it can also be distinguished whether the user is using 2G, 3G or 4G. Of course, after you obtain this data, you can also calculate which company the user is using, mobile, China Unicom, or telecom, of course, only in China. Moreover, after the virtual operators start to go public, they cannot tell whether they are from JD.com, Gome, or Suning, but you can know whether your mobile phone number uses China Unicom or Mobile.
Finally, I will post some information I have collected and sorted out, so you can refer to it.
- GPRS 2G(2.5) General Packet Radia Service 114kbps
- EDGE 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
- UMTS 3G WCDMA Universal Mobile Telecommunication System Complete 3G mobile communication technology standards
- CDMA 2G Telecom Code Division Multiple Access Code Division Multiple Access
- EVDO_0 3G (EVDO full CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps belongs to 3G
- EVDO_A 3G 1.8mbps - 3.1mbps belongs to 3G transition, 3.5G
- 1xRTT 2G CDMA2000 1xRTT (RTT - Radio Transmission Technology) 144kbps 2G Transition,
- HSDPA 3.5G High Speed Downlink Packet Access 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
- HSUPA 3.5G High Speed Uplink Packet Access High-speed Uplink Packet Access 1.4 - 5.8 mbps
- HSPA 3G (HSDPA, HSUPA) High Speed Packet Access
- IDEN 2G Integrated Dispatch Enhanced Networks Integrated Digital Enhanced Networks (Among 2G, from Wikipedia)
- EVDO_B 3G EV-DO 14.7Mbps downlink 3.5G
- LTE 4G Long Term Evolution FDD-LTE and TDD-LTE, 3G transition, the upgraded version of LTE Advanced is 4G
- EHRPD 3G CDMA2000 intermediate product to LTE 4G Evolved High Rate Packet Data HRPD upgrade
- HSPAP 3G HSPAP is faster than HSDPA
Example:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class NetWorkUtil { public static boolean isWifiAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) ConfigManager .getContext().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = (); return (networkInfo != null && () && networkInfo .getType() == ConnectivityManager.TYPE_WIFI); } /** * Get the MAC address * * @param context * @return */ public static String getMacAddress(Context context) { if (context == null) { return ""; } String localMac = null; if (isWifiAvailable()) { localMac = getWifiMacAddress(context); } if (localMac != null && () > 0) { localMac = (":", "-").toLowerCase(); return localMac; } localMac = getMacFromCallCmd(); if (localMac != null) { localMac = (":", "-").toLowerCase(); } return localMac; } private static String getWifiMacAddress(Context context) { String localMac = null; try { WifiManager wifi = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo info = (); if (()) { localMac = (); if (localMac != null) { localMac = (":", "-").toLowerCase(); return localMac; } } } catch (Exception e) { (); } return null; } /** * Get the mac address through callCmd("busybox ifconfig","HWaddr") * * @attention The device requires the busybox tool installed * @return Mac Address */ private static String getMacFromCallCmd() { String result = ""; result = callCmd("busybox ifconfig", "HWaddr"); if (result == null || () <= 0) { return null; } ("tag", "cmd result : " + result); // Analyze the data of this line // For example: eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67 if (() > 0 && ("HWaddr") == true) { String Mac = (("HWaddr") + 6, () - 1); if (() > 1) { result = (" ", ""); } } return result; } public static String callCmd(String cmd, String filter) { String result = ""; String line = ""; try { Process proc = ().exec(cmd); InputStreamReader is = new InputStreamReader(()); BufferedReader br = new BufferedReader(is); // Execute the command cmd, and only the line containing the filter in the result is taken while ((line = ()) != null && (filter) == false) { } result = line; } catch (Exception e) { (); } return result; } /** * Is the network available? * * @param context * @return */ public static boolean IsNetWorkEnable(Context context) { try { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { (context, "Unable to connect to the network"); return false; } NetworkInfo info = (); if (info != null && ()) { // Determine whether the current network is connected if (() == ) { return true; } } } catch (Exception e) { (); } (context, "Unable to connect to the network"); return false; } private static final int NETWORK_TYPE_UNAVAILABLE = -1; // private static final int NETWORK_TYPE_MOBILE = -100; private static final int NETWORK_TYPE_WIFI = -101; private static final int NETWORK_CLASS_WIFI = -101; private static final int NETWORK_CLASS_UNAVAILABLE = -1; /** Unknown network class. */ private static final int NETWORK_CLASS_UNKNOWN = 0; /** Class of broadly defined "2G" networks. */ private static final int NETWORK_CLASS_2_G = 1; /** Class of broadly defined "3G" networks. */ private static final int NETWORK_CLASS_3_G = 2; /** Class of broadly defined "4G" networks. */ private static final int NETWORK_CLASS_4_G = 3; private static DecimalFormat df = new DecimalFormat("#.##"); // Adapt to lower version mobile phones /** Network type is unknown */ public static final int NETWORK_TYPE_UNKNOWN = 0; /** Current network is GPRS */ public static final int NETWORK_TYPE_GPRS = 1; /** Current network is EDGE */ public static final int NETWORK_TYPE_EDGE = 2; /** Current network is UMTS */ public static final int NETWORK_TYPE_UMTS = 3; /** Current network is CDMA: Either IS95A or IS95B */ public static final int NETWORK_TYPE_CDMA = 4; /** Current network is EVDO revision 0 */ public static final int NETWORK_TYPE_EVDO_0 = 5; /** Current network is EVDO revision A */ public static final int NETWORK_TYPE_EVDO_A = 6; /** Current network is 1xRTT */ public static final int NETWORK_TYPE_1xRTT = 7; /** Current network is HSDPA */ public static final int NETWORK_TYPE_HSDPA = 8; /** Current network is HSUPA */ public static final int NETWORK_TYPE_HSUPA = 9; /** Current network is HSPA */ public static final int NETWORK_TYPE_HSPA = 10; /** Current network is iDen */ public static final int NETWORK_TYPE_IDEN = 11; /** Current network is EVDO revision B */ public static final int NETWORK_TYPE_EVDO_B = 12; /** Current network is LTE */ public static final int NETWORK_TYPE_LTE = 13; /** Current network is eHRPD */ public static final int NETWORK_TYPE_EHRPD = 14; /** Current network is HSPA+ */ public static final int NETWORK_TYPE_HSPAP = 15; /** * Format size * * @param size * @return */ public static String formatSize(long size) { String unit = "B"; float len = size; if (len > 900) { len /= 1024f; unit = "KB"; } if (len > 900) { len /= 1024f; unit = "MB"; } if (len > 900) { len /= 1024f; unit = "GB"; } if (len > 900) { len /= 1024f; unit = "TB"; } return (len) + unit; } public static String formatSizeBySecond(long size) { String unit = "B"; float len = size; if (len > 900) { len /= 1024f; unit = "KB"; } if (len > 900) { len /= 1024f; unit = "MB"; } if (len > 900) { len /= 1024f; unit = "GB"; } if (len > 900) { len /= 1024f; unit = "TB"; } return (len) + unit + "/s"; } public static String format(long size) { String unit = "B"; float len = size; if (len > 1000) { len /= 1024f; unit = "KB"; if (len > 1000) { len /= 1024f; unit = "MB"; if (len > 1000) { len /= 1024f; unit = "GB"; } } } return (len) + "\n" + unit + "/s"; } /** * Get the operator * * @return */ public static String getProvider() { String provider = "unknown"; try { TelephonyManager telephonyManager = (TelephonyManager) ConfigManager .getContext().getSystemService(Context.TELEPHONY_SERVICE); String IMSI = (); ("tag", ":" + IMSI); if (IMSI == null) { if (TelephonyManager.SIM_STATE_READY == telephonyManager .getSimState()) { String operator = (); ("tag", ":" + operator); if (operator != null) { if (("46000") || ("46002") || ("46007")) { provider = "China Mobile"; } else if (("46001")) { provider = "China Unicom"; } else if (("46003")) { provider = "China Telecom"; } } } } else { if (("46000") || ("46002") || ("46007")) { provider = "China Mobile"; } else if (("46001")) { provider = "China Unicom"; } else if (("46003")) { provider = "China Telecom"; } } } catch (Exception e) { (); } return provider; } /** * Get network type * * @return */ public static String getCurrentNetworkType() { int networkClass = getNetworkClass(); String type = "unknown"; switch (networkClass) { case NETWORK_CLASS_UNAVAILABLE: type = "none"; break; case NETWORK_CLASS_WIFI: type = "Wi-Fi"; break; case NETWORK_CLASS_2_G: type = "2G"; break; case NETWORK_CLASS_3_G: type = "3G"; break; case NETWORK_CLASS_4_G: type = "4G"; break; case NETWORK_CLASS_UNKNOWN: type = "unknown"; break; } return type; } private static int getNetworkClassByType(int networkType) { switch (networkType) { case NETWORK_TYPE_UNAVAILABLE: return NETWORK_CLASS_UNAVAILABLE; case NETWORK_TYPE_WIFI: return NETWORK_CLASS_WIFI; case NETWORK_TYPE_GPRS: case NETWORK_TYPE_EDGE: case NETWORK_TYPE_CDMA: case NETWORK_TYPE_1xRTT: case NETWORK_TYPE_IDEN: return NETWORK_CLASS_2_G; case NETWORK_TYPE_UMTS: case NETWORK_TYPE_EVDO_0: case NETWORK_TYPE_EVDO_A: case NETWORK_TYPE_HSDPA: case NETWORK_TYPE_HSUPA: case NETWORK_TYPE_HSPA: case NETWORK_TYPE_EVDO_B: case NETWORK_TYPE_EHRPD: case NETWORK_TYPE_HSPAP: return NETWORK_CLASS_3_G; case NETWORK_TYPE_LTE: return NETWORK_CLASS_4_G; default: return NETWORK_CLASS_UNKNOWN; } } private static int getNetworkClass() { int networkType = NETWORK_TYPE_UNKNOWN; try { final NetworkInfo network = ((ConnectivityManager) ConfigManager .getContext() .getSystemService(Context.CONNECTIVITY_SERVICE)) .getActiveNetworkInfo(); if (network != null && () && ()) { int type = (); if (type == ConnectivityManager.TYPE_WIFI) { networkType = NETWORK_TYPE_WIFI; } else if (type == ConnectivityManager.TYPE_MOBILE) { TelephonyManager telephonyManager = (TelephonyManager) ConfigManager .getContext().getSystemService( Context.TELEPHONY_SERVICE); networkType = (); } } else { networkType = NETWORK_TYPE_UNAVAILABLE; } } catch (Exception ex) { (); } return getNetworkClassByType(networkType); } public static String getWifiRssi() { int asu = 85; try { final NetworkInfo network = ((ConnectivityManager) ConfigManager .getContext() .getSystemService(Context.CONNECTIVITY_SERVICE)) .getActiveNetworkInfo(); if (network != null && () && ()) { int type = (); if (type == ConnectivityManager.TYPE_WIFI) { WifiManager wifiManager = (WifiManager) ConfigManager .getContext() .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = (); if (wifiInfo != null) { asu = (); } } } } catch (Exception e) { (); } return asu + "dBm"; } public static String getWifiSsid() { String ssid = ""; try { final NetworkInfo network = ((ConnectivityManager) ConfigManager .getContext() .getSystemService(Context.CONNECTIVITY_SERVICE)) .getActiveNetworkInfo(); if (network != null && () && ()) { int type = (); if (type == ConnectivityManager.TYPE_WIFI) { WifiManager wifiManager = (WifiManager) ConfigManager .getContext() .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = (); if (wifiInfo != null) { ssid = (); if (ssid == null) { ssid = ""; } ssid = ("\"", ""); } } } } catch (Exception e) { (); } return ssid; } /** * Check the sim card status * * @param ctx * @return */ public static boolean checkSimState() { TelephonyManager tm = (TelephonyManager) () .getSystemService(Context.TELEPHONY_SERVICE); if (() == TelephonyManager.SIM_STATE_ABSENT || () == TelephonyManager.SIM_STATE_UNKNOWN) { return false; } return true; } /** * Get imei */ public static String getImei() { TelephonyManager mTelephonyMgr = (TelephonyManager) ConfigManager .getContext().getSystemService(Context.TELEPHONY_SERVICE); String imei = (); if (imei == null) { imei = "000000000000000"; } return imei; } public static String getPhoneImsi() { TelephonyManager mTelephonyMgr = (TelephonyManager) ConfigManager .getContext().getSystemService(Context.TELEPHONY_SERVICE); return (); } public static CellInfo getNetInfo() { CellInfo info = new CellInfo(); try { TelephonyManager mTelephonyManager = (TelephonyManager) ConfigManager .getContext().getSystemService(Context.TELEPHONY_SERVICE); String operator = (); if (operator != null) { /** Get MCC and MNC through operator */ if (() > 3) { String mcc = (0, 3); String mnc = (3); (mcc); (mnc); } } int lac = 0; int cellId = 0; int phoneType = (); if (phoneType == TelephonyManager.PHONE_TYPE_GSM) { GsmCellLocation location = (GsmCellLocation) mTelephonyManager .getCellLocation(); /** Get China Mobile and Unicom LAC and cellID through GsmCellLocation */ lac = (); cellId = (); } else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) { CdmaCellLocation location = (CdmaCellLocation) mTelephonyManager .getCellLocation(); lac = (); cellId = (); cellId /= 16; } if (lac == 0 || cellId == 0) { List<NeighboringCellInfo> infos = mTelephonyManager .getNeighboringCellInfo(); int lc = 0; int ci = 0; int rssi = 0; for (NeighboringCellInfo cell : infos) { //Cycle according to the total number of neighbors if (lc == 0 || ci == 0) { lc = (); ci = (); rssi = (); } // (" LAC : " + ()); // // Take out the LAC of the current neighbor area // (" CID : " + ()); // // Take out the CID of the current neighbor area // (" BSSS : " + (-113 + 2 * ()) + // "\n"); // Get the signal strength of the neighboring base station } rssi = -113 + 2 * rssi; } } catch (Exception e) { (); } return info; } }
I hope this article will be helpful to everyone to learn Android software programming.