
做一波获取手机卡LET的信息操作。看了一波源码写出来的一些东西
首先需要的一些权限(危险权限动态获取一下,之前的里面有):
<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"/>//首先获取手机管理者类
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);//获取设备编号
tm.getDeviceId();//SIM卡提供商的ISO国家代码
tm.getSimCountryIso();//获取SIM卡序列号
tm.getSimSerialNumber();//获取网络运营商代号(MCC+MNC)
tm.getNetworkOperator();//获取网络运营商名称
tm.getNetworkOperatorName();//获取设备当前所在位置
tm.getCellLocation();//获取cId,lac,psc,基于位置信息获取
CellLocation cellLocation = tm.getCellLocation();
if(cellLocation instanceof GsmCellLocation) {
GsmCellLocation location = (GsmCellLocation)cellLocation;
int cid = location.getCid();
int lac = location.getLac();
int psc = location.getPsc();
}//获取手机类型
tm.getPhoneType();//获取手机号码
tm.getLine1Number();//获取国际长途区号
tm.getNetworkCountryIso();//获取连接状态
tm.getDataState();//获取网络类型(SDK版本29,才有5G,5G代号NR)
tm.getNetworkType();//获取临近小区信息
之前的tm.getNeighboringCellInfo();这个方法在SDK29版本里删除掉了,所以得用另一种方法获取。
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等信息
//注册一个监听,利用反射拿到这些数据,在监听后拿数据即可
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;
}
}
}
}//监听使用方法,需要调用一下
MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();tm.listen(myPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185296.html原文链接:https://javaforall.cn