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


    同一张卡的网络信号,有2G,3G,4G,5G等多种

    这里主要实现4G和5G信号强度的获取,2G和3G的获取方式雷同

    但由于现在已经不常用了,所以不再写多余的代码

    
    	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;
    	    }
    	}
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
  • 相关阅读:
    Jest单元测试相关
    面试必考精华版Leetcode236. 二叉树的最近公共祖先
    开发笔记 | 编译报错 | error: expected identifier or ‘(‘ before ‘return‘
    HCIA-Access V2.5 华为认证接入网络工程师学习笔记第二章
    渗透测试基础(四) MS08-067 漏洞攻击
    Anaconda prompt 启动jupyter时,出现版本的问题
    PyTorch笔记 - Attention Is All You Need (1)
    尚硅谷大数据数仓项目superset db upgrade三个报错解答
    Py使用requests爬虫1(基础+入门)
    「Linux系列」Linux 如何学习用户和用户组管理
  • 原文地址:https://blog.csdn.net/u013718730/article/details/127651704