看到question和答案后(感谢方式)我写了这个代码与答案几乎相同:
try {
List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();
for (CellInfo cellInfo : cellInfoList) {
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity();
final CellSignalStrengthGsm gsm = ((CellInfoGsm) cellInfo).getCellSignalStrength();
rsrpValue = gsm.getDbm();
pciValue = cellIdentity.getCid();
} else if (cellInfo instanceof CellInfoCdma) {
CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
CellIdentityCdma cellIdentity = cellInfoCdma.getCellIdentity();
pciValue = cellIdentity.getBasestationId();
} else if (cellInfo instanceof CellInfoLte){
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
CellIdentityLte cellIdentity = cellInfoLte.getCellIdentity();
pciValue = cellIdentity.getPci();
} else {
throw new Exception("Unknown type of cell signal!");
}
}
} catch (Exception e) {
Log.e(TAG, "Unable to obtain cell signal information", e);
}
但是当我为GSM显示rsrpValue或pciValue时,我总是得到最大整数值(2147483647)。我在带有API 17的手机上试过这个。我的代码中有什么问题吗?
由于
答案 0 :(得分:1)
尝试使用旧的PhoneStateListener来监听信号强度的变化。这对我有用。 使用它来注册监听器:
private TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int currentSignalStrength = 0;
int asu = 0;
telManager.listen(new SignalStrengthListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
这是真正的倾听者:
private class SignalStrengthListener extends PhoneStateListener{
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
if (telManager.getPhoneType()== TelephonyManager.PHONE_TYPE_CDMA)
currentSignalStrength = signalStrength.getCdmaDbm();
else
asu = signalStrength.getGsmSignalStrength();
}
}
在Xperia Z上测试过,使用CellInfoGsm会遇到同样的问题。