做一波获取手机卡LET的信息操作。看了一波源码写出来的一些东西
首先需要的一些权限(危险权限动态获取一下,之前的里面有):
代码语言:javascript复制<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
//首先获取手机管理者类
代码语言:javascript复制tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//获取设备编号
代码语言:javascript复制tm.getDeviceId();
//SIM卡提供商的ISO国家代码
代码语言:javascript复制tm.getSimCountryIso();
//获取SIM卡序列号
代码语言:javascript复制tm.getSimSerialNumber();
//获取网络运营商代号(MCC MNC)
代码语言:javascript复制tm.getNetworkOperator();
//获取网络运营商名称
代码语言:javascript复制tm.getNetworkOperatorName();
//获取设备当前所在位置
代码语言:javascript复制tm.getCellLocation();
//获取cId,lac,psc,基于位置信息获取
代码语言:javascript复制CellLocation cellLocation = tm.getCellLocation();
if(cellLocation instanceof GsmCellLocation) {
GsmCellLocation location = (GsmCellLocation)cellLocation;
int cid = location.getCid();
int lac = location.getLac();
int psc = location.getPsc();
}
//获取手机类型
代码语言:javascript复制tm.getPhoneType();
//获取手机号码
代码语言:javascript复制tm.getLine1Number();
//获取国际长途区号
代码语言:javascript复制tm.getNetworkCountryIso();
//获取连接状态
代码语言:javascript复制tm.getDataState();
//获取网络类型(SDK版本29,才有5G,5G代号NR)
代码语言:javascript复制tm.getNetworkType();
//获取临近小区信息
之前的tm.getNeighboringCellInfo();这个方法在SDK29版本里删除掉了,所以得用另一种方法获取。
代码语言:javascript复制List<CellInfo> allCellInfo = tm.getAllCellInfo();
//集合返回的第一个数据,allCellInfo.get(0)就是当前小区的数据
String ss = allCellInfo.toString();
for(CellInfo cellInfo : allCellInfo){
if(cellInfo instanceof CellInfoGsm) {
CellInfoGsm infoGsm = (CellInfoGsm) cellInfo;
CellIdentityGsm cellIdentity = infoGsm.getCellIdentity();
int cid = cellIdentity.getCid();
int lac = cellIdentity.getLac();
int psc = cellIdentity.getPsc();
int arfcn = cellIdentity.getArfcn();
int rsrp = infoGsm.getCellSignalStrength().getDbm();
str = "2G=====cid:" cid "=====lac:" lac "=====psc:" psc "=====arfcn:" arfcn "=====rsrp:" rsrp "n";
}else if(cellInfo instanceof CellInfoWcdma){
CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
int cid = cellInfoWcdma.getCellIdentity().getCid();
int lac = cellInfoWcdma.getCellIdentity().getLac();
int psc = cellInfoWcdma.getCellIdentity().getPsc();
int rsrp = cellInfoWcdma.getCellSignalStrength().getDbm();
serverCellInfo.asulevel = cellInfoWcdma.getCellSignalStrength().getAsuLevel();
str = "3G=====cid:" cid "=====lac:" lac "=====psc:" psc "=====rsrp:" rsrp "n";
}else if(cellInfo instanceof CellInfoLte){
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
int cid = cellInfoLte.getCellIdentity().getCi();
int pci = cellInfoLte.getCellIdentity().getPci();
int tac = cellInfoLte.getCellIdentity().getTac();
int earfcn = cellInfoLte.getCellIdentity().getEarfcn();
int rsrp = cellInfoLte.getCellSignalStrength().getRsrp();
int rsrq = cellInfoLte.getCellSignalStrength().getRsrq();
// Object rssi = cellInfoLte.getCellSignalStrength().getRssi();
Object rssnr = cellInfoLte.getCellSignalStrength().getRssnr();
Object bandwidth = cellInfoLte.getCellIdentity().getBandwidth();
str = "4G=====cid:" cid "=====pci:" pci "=====tac:" tac "=====earfcn:" earfcn "=====rsrp:" rsrp "=====rsrq:"
rsrq "=====rssnr:" rssnr "=====bandWidth:" bandwidth "n";
}else if(cellInfo instanceof CellInfoNr){
CellInfoNr cellInfoNr = (CellInfoNr) cellInfo;
int asuLevel = cellInfoNr.getCellSignalStrength().getAsuLevel();
int rsrp = cellInfoNr.getCellSignalStrength().getDbm();
int level = cellInfoNr.getCellSignalStrength().getLevel();
str = "5G=====asuLevel:" asuLevel "=====rsrp:" rsrp "=====level:" level;
}
}
注:商用手机无法获得5G下的小区信息,可以检测到是NR(5G)网络,但是即使在5G情况下获得的仍是4G锚点小区的信息!!!想要获取需要跟手机厂家去购买获取Root过的手机。
//上面那个方法没法获取rssi,总是报错,获取sinr,cqi,rsrp,rssi等信息
//注册一个监听,利用反射拿到这些数据,在监听后拿数据即可
代码语言:javascript复制class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
if (phoneGeneralInfo.ratType == TelephonyManager.NETWORK_TYPE_LTE) {
try {
int rssi = (Integer) signalStrength.getClass().getMethod("getLteSignalStrength").invoke(signalStrength);
int rsrp = (Integer) signalStrength.getClass().getMethod("getLteRsrp").invoke(signalStrength);
int rsrq = (Integer) signalStrength.getClass().getMethod("getLteRsrq").invoke(signalStrength);
int sinr = (Integer) signalStrength.getClass().getMethod("getLteRssnr").invoke(signalStrength);
int cqi = (Integer) signalStrength.getClass().getMethod("getLteCqi").invoke(signalStrength);
} catch (Exception e) {
e.printStackTrace();
return;
}
} else if (phoneGeneralInfo.ratType == TelephonyManager.NETWORK_TYPE_GSM) {
try {
serverCellInfo.rssi = signalStrength.getGsmSignalStrength();
serverCellInfo.rsrp = (Integer) signalStrength.getClass().getMethod("getGsmDbm").invoke(signalStrength);
serverCellInfo.asulevel = (Integer) signalStrength.getClass().getMethod("getAsuLevel").invoke(signalStrength);
} catch (Exception e) {
e.printStackTrace();
return;
}
} else if (phoneGeneralInfo.ratType == TelephonyManager.NETWORK_TYPE_TD_SCDMA) {
try {
int rssi = (Integer) signalStrength.getClass().getMethod("getTdScdmaLevel").invoke(signalStrength);
int rsrp = (Integer) signalStrength.getClass().getMethod("getTdScdmaDbm").invoke(signalStrength);
int asulevel = (Integer) signalStrength.getClass().getMethod("getAsuLevel").invoke(signalStrength);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}
//监听使用方法,需要调用一下
代码语言:javascript复制MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
代码语言:javascript复制tm.listen(myPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185296.html原文链接:https://javaforall.cn