应用能够获取4G的信号强度,却无法获取5G的信号强度,通过修改https://github.com/microg/UnifiedNlp项目实现,
信号监听方法用的phoneStateListener方式
telephonyManager=((TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE));
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CELL_INFO|PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
捕获onCellInfoChanged实现
if (phoneStateListener == null) {
Handler mainHandler = new Handler(context.getMainLooper());
mainHandler.post(() -> {
phoneStateListener = new PhoneStateListener() {
@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
System.out.println(JSON.toJSONString(cellInfo));
if (cellInfo != null && !cellInfo.isEmpty()) {
onCellsChanged(cellInfo);
} else if (supportsCellInfoChanged) {
supportsCellInfoChanged = false;
onSignalStrengthsChanged(null);
}
}
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
if (!supportsCellInfoChanged) {
doScan();
}
}
};
registerPhoneStateListener();
});
} else {
registerPhoneStateListener();
}
并针对5GNR信号进行处理
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && info instanceof CellInfoNr){
CellIdentityNr identity = (CellIdentityNr) ((CellInfoNr) info).getCellIdentity();
int Mcc=(identity.getMccString() != null) ? Integer.valueOf(identity.getMccString()) : CellInfo.UNAVAILABLE;
int Mnc=(identity.getMncString() != null) ? Integer.valueOf(identity.getMncString()) : CellInfo.UNAVAILABLE;
// if ( Mcc== Integer.MAX_VALUE) return null;
CellSignalStrengthNr strength = (CellSignalStrengthNr) ((CellInfoNr) info).getCellSignalStrength();
return new Cell(Cell.CellType.NR, Mcc, Mnc,
identity.getTac(), identity.getNci(), identity.getPci(), strength.getDbm());
}
然而,采集到的信号只有4GLTE的信号强度-76(冒号后面的数),5GNR的信号强度为Integer.MAX,也就是没有赋值
使用的小米10、红米k40pro以及华为p40测试结果都是这样,各位觉得问题出在哪里?