【Android】获取手机上所有电话卡的运营商和信号强度

时间:2025-01-29 07:49:02
package com.android.architecture; import android.content.Context; import android.telephony.CellIdentityLte; import android.telephony.CellIdentityNr; import android.telephony.CellInfo; import android.telephony.CellInfoLte; import android.telephony.CellInfoNr; import android.telephony.CellSignalStrengthLte; import android.telephony.CellSignalStrengthNr; import android.telephony.TelephonyManager; import java.util.ArrayList; import java.util.List; @SuppressWarnings("all") public class SimInfo { public String operatorCode; public String operatorName; public int signalLevel; public String signalLevelName; @Override public String toString() { return operatorName + " : " + signalLevelName; } public void setOperatorName() { if (operatorCode.equals("46000")) operatorName = "中国移动"; else if (operatorCode.equals("46002")) operatorName = "中国移动"; else if (operatorCode.equals("46004")) operatorName = "中国移动"; else if (operatorCode.equals("46007")) operatorName = "中国移动"; else if (operatorCode.equals("46008")) operatorName = "中国移动"; else if (operatorCode.equals("46003")) operatorName = "中国电信"; else if (operatorCode.equals("46005")) operatorName = "中国电信"; else if (operatorCode.equals("46011")) operatorName = "中国电信"; else if (operatorCode.equals("46001")) operatorName = "中国联通"; else if (operatorCode.equals("46006")) operatorName = "中国联通"; else if (operatorCode.equals("46009")) operatorName = "中国联通"; else if (operatorCode.equals("46020")) operatorName = "中国铁通"; else operatorName = "其它运营商"; } public void setSignalLevelName() { if (signalLevel == 4) signalLevelName = "好"; else if (signalLevel == 3) signalLevelName = "中"; else if (signalLevel == 2) signalLevelName = "差"; else if (signalLevel == 1) signalLevelName = "极差"; else signalLevelName = "无信号"; } //获取所有SIM卡的信号强度信息 public static List<SimInfo> getAllSimInfo(Context context) { TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class); List<SimInfo> simInfoList = new ArrayList(); List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo(); for (CellInfo cellInfo : cellInfoList) { if (!cellInfo.isRegistered()) continue; //4G if (cellInfo instanceof CellInfoLte) { CellInfoLte lte = (CellInfoLte) cellInfo; CellIdentityLte identity = lte.getCellIdentity(); CellSignalStrengthLte strength = lte.getCellSignalStrength(); SimInfo simInfo = new SimInfo(); simInfo.operatorCode = identity.getMobileNetworkOperator(); simInfo.signalLevel = strength.getLevel(); simInfo.setOperatorName(); simInfo.setSignalLevelName(); simInfoList.add(simInfo); } //5G if (cellInfo instanceof CellInfoNr) { CellInfoNr nr = (CellInfoNr) cellInfo; CellIdentityNr identity = (CellIdentityNr) nr.getCellIdentity(); CellSignalStrengthNr strength = (CellSignalStrengthNr) nr.getCellSignalStrength(); SimInfo simInfo = new SimInfo(); simInfo.operatorCode = "" + identity.getMccString() + identity.getMncString(); simInfo.signalLevel = strength.getLevel(); simInfo.setOperatorName(); simInfo.setSignalLevelName(); simInfoList.add(simInfo); } } return simInfoList; } }