我想存储细胞信号强度,我这样做:
private class GetRssi extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
Variables.signal = signalStrength.getGsmSignalStrength();
}
}
好的,但只有在它发生变化时才会运行。我需要当前的信号强度。
有没有办法只询问当前的信号强度?
答案 0 :(得分:18)
添加了CellSignalStrengthGsm()在API级别17中添加
CellSignalStrengthGsm()。getDbm()会给你信号强度为dBm
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); //This will give info of all sims present inside your mobile
if(cellInfos!=null){
for (int i = 0 ; i<cellInfos.size(); i++){
if (cellInfos.get(i).isRegistered()){
if(cellInfos.get(i) instanceof CellInfoWcdma){
CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthWcdma cellSignalStrengthWcdma = cellInfoWcdma.getCellSignalStrength();
strength = String.valueOf(cellSignalStrengthWcdma.getDbm());
}else if(cellInfos.get(i) instanceof CellInfoGsm){
CellInfoGsm cellInfogsm = (CellInfoGsm) telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellInfogsm.getCellSignalStrength();
strength = String.valueOf(cellSignalStrengthGsm.getDbm());
}else if(cellInfos.get(i) instanceof CellInfoLte){
CellInfoLte cellInfoLte = (CellInfoLte) telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();
strength = String.valueOf(cellSignalStrengthLte.getDbm());
}
}
}
return strength;
}
您可以从以下方面了解更多信息: https://developer.android.com/reference/android/telephony/CellInfo.html
CellInfoCdma,CellInfoGsm,CellInfoLte,CellInfoWcdma是CellInfo的子类。其中提供了与您的移动网络相关的所有信息。
答案 1 :(得分:17)
在API 17中添加的TelephonyManager中的getAllCellInfo()方法可能是一个很好的解决方案。使用示例:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
// for example value of first element
CellInfoGsm cellInfoGsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
答案 2 :(得分:0)
如果您使用的 SDK 版本 > 28,您可以像这样使用 getSignalStrength() 函数:
telephonyManager.getSignalStrength().getLevel();
这是一个完全实现的函数:
TelephonyManager telephonyManager;
...
int getCellularSignalStrength() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if(telephonyManager == null)
telephonyManager = (TelephonyManager) common.appContext.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getSignalStrength().getLevel();
}
else
{
logRecorder.addWarningLog("Cannot read signal lvl on API lvl " + android.os.Build.VERSION.SDK_INT);
return -1;
}
}