android 获取网络制式_Android
2026-11-29 08:16:47

在Android中,取网可(ke)以通过TelephonyManager类获取网络制式。络制首先需要获取到TelephonyManager的取网实例,然后通过getNetworkType()方法获取网络类型,络制最(zui)后通过(guo)getNetworkOperatorName()方法获取运营商名(ming)称。取网

在Android中(zhong),络制获取网络制式可以通过TelephonyManager类来实现,取网以下是络制(zhi)详细的步骤和小标题:(图片来源网络,侵删)

1、取网导入相关类

android 获取网络制式_Android

需要在AndroidManifest.xml文件中添加访问电话服务的络制权限:

android 获取网络制式_Android

“`xml

android 获取网络制式_Android

<usespermission android:name="android.permission.ACCESS_NETWORK_STATE" />

<usespermission android:name="android.permission.READ_PHONE_STATE" />

“`

2、获取TelephonyManager实例

(图片(pian)来源网络,取(qu)网侵删)

在需要获取(qu)网络制式的络制Activity或Fragment中(zhong),通过以下方式获取TelephonyManager实例:

“`java

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

“`

3、取网获取网络制式信息

使用TelephonyManager的络制getNetworkType()方法可以获取当前连接的(de)网络制式,该方法返回(hui)一个整(zheng)数值,取网表示网络制式的(de)类型,以下是(shi)常(chang)见的网络制式及其对应的整数值:

| 网络制式 | 整数值 |

(图片来源网络,侵删)

| | |

| GSM | 0 |

| CDMA | 1 |

| WCDMA | 2 |

| LTE | 4 |

| EvDO | 5 |

| HSPA+ | 6 |

| HSPA | 7 |

| HSPAPRoaming | 8 |

| GSM/UMTS/HSPA+/DCHSDPA/HSUPA | 9 |

| GSM/UMTS/LTE/CDMA/EvDo/HSPA+/HSDPA/HSUPA | 10 |

根据获取到的整数值,可以将网络制式转换为相应的名称,以下是将整数值转换为网络制式(shi)的示例代(dai)码:

“`java

int networkType = telephonyManager.getNetworkType();

String networkTypeName = "未知";

switch (networkType) {

case TelephonyManager.NETWORK_TYPE_GPRS: // GSM or edge radio access network (GERAN)

networkTypeName = "GSM";

break;

case TelephonyManager.NETWORK_TYPE_EDGE: // enhanced data rates for global evolution (EDGE) radio access network (GERAN)

networkTypeName = "EDGE";

break;

case TelephonyManager.NETWORK_TYPE_CDMA: // code division multiple="" access (CDMA) radio access network (UTRAN) or evolved packet core (EPC) network type is EvDo revision A or B, using HSPA technology over the air interface (UTRA) or EUTRAN, respectively.

networkTypeName = "CDMA";

break;

case TelephonyManager.NETWORK_TYPE_EVDO_0: // EvolutionData Optimized (EVDO) revision 0 network type is used on the air interface of the Evolved HighSpeed Packed Access (HSPA+) Evolved UTRA and Evolved UTRATDD networks, with base stations based on the Evolved HighSpeed Downlink Packet Access (HSDPA) and Evolved HighSpeed Uplink Packet Access (HSUPA) technologies. It is also used on the air interface of the LongTerm Evolution (LTE) network with base stations based on the EvolvedUMTS Terrestrial Radio Access Network (EUTRAN). The term "EvDO" is used interchangeably with "HRPD" in this context.

networkTypeName = "EVDO";

break;

case TelephonyManager.NETWORK_TYPE_EVDO_A: // EvolutionData Optimized (EVDO) revision A network type is used on the air interface of the Evolved HighSpeed Packed Access (HSPA+) Evolved UTRA and Evolved UTRATDD networks, with base stations based on the Evolved HighSpeed Downlink Packet Access (HSDPA) and Evolved HighSpeed Uplink Packet Access (HSUPA) technologies. It is also used on the air interface of the LongTerm Evolution (LTE) network with base stations based on the EvolvedUMTS Terrestrial Radio Access Network (EUTRAN). The term "EvDO" is used interchangeably with "HRPD" in this context.

networkTypeName = "EVDO";

break;

case TelephonyManager.NETWORK_TYPE_HSPAP: // HighSpeed Packet Access Plus (HSPA+) network type is used on the air interface of the LongTerm Evolution (LTE) network with base stations based on the EvolvedUMTS Terrestrial Radio Access Network (EUTRAN). The term "HSPA+" is used interchangeably with "HSPA+" in this context.

networkTypeName = "HSPA+";

break;

case TelephonyManager.NETWORK_TYPE_HSDPA: // HighSpeed Downlink Packet Access (HSDPA) network type is used on the air interface of the LongTerm Evolution (LTE) network with base stations based on the EvolvedUMTS Terrestrial Radio Access Network (EUTRAN). The term "HSDPA" is used interchangeably with "HSPA" in this context.

networkTypeName = "HSDPA";

break;

case TelephonyManager.NETWORK_TYPE_HSUPA: // HighSpeed Uplink Packet Access (HSUPA) network type is used on the air interface of the LongTerm Evolution (LTE) network with base stations based on the EvolvedUMTS Terrestrial Radio Access Network (EUTRAN). The term "HSUPA" is used interchangeably with "HSPA" in this context.

networkTypeName = "HSUPA";

break;

case TelephonyManager.NETWORK_TYPE_UMTS: // universal mobile telecommunication system (UMTS) radio access network (UTRAN) or evolved packet core (EPC) network type is either high speed packet access plus (HSPA+), high speed downlink packet access (HSDPA), high speed uplink packet access (HSUPA), or long term evolution (LTE). The term "UMTS" is used interchangeably with "3G" in this context.

networkTypeName = "UMTS";

break;

case TelephonyManager.NETWORK_TYPE_UNKNOWN: // unknown network type, should not happen in practice but might occur if a new network type was added to Android after this code was written. This value can be safely ignored by applications.

networkTypeName = "未知";

break;

default: // other network types not listed here are treated as unknown network types and fall back to this default value. This value can be safely ignored by applications.

下面是一个简单的介绍,用(yong)于展(zhan)示在Android设备上获取网络制式的方法:

网络制式 获取方法
2G (GSM/EDGE)TelephonyManager.NETWORK_TYPE_GPRS 或TelephonyManager.NETWORK_TYPE_EDGE
3G (UMTS)TelephonyManager.NETWORK_TYPE_UMTS
4G (LTE)TelephonyManager.NETWORK_TYPE_LTE
5GTelephonyManager.NETWORK_TYPE_NR (需要Android API 29+)

以下是相关代码示例:

import android.telephony.TelephonyManager;// 获取TelephonyManager实(shi)例TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);// 获取网络类型int networkType = telephonyManager.getNetworkType();// 根据不同的网络类型进行判断switch (networkType) {  case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: // 2G网络 break; case TelephonyManager.NETWORK_TYPE_UMTS: // 3G网络 break; case TelephonyManager.NETWORK_TYPE_LTE: // 4G网络 break; case TelephonyManager.NETWORK_TYPE_NR: // 5G网络(需要Android API 29+) break; default: // 其他网络类型 break;}

请注意,从Android API 29 (Android 10) 开始,应用程序需要拥有(you)READ_PHONE_STATE权限才能访问网络类型(xing)信息,为了确保应用程序的兼容(rong)性和隐私安全,请务必遵循最新的Android开发指南(nan)。

(作者:网站建设)