如何获得当前的细胞信号强度?

Ada*_*gyi 13 android telephony

我想存储细胞信号强度,我这样做:

private class GetRssi extends PhoneStateListener {
    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        Variables.signal = signalStrength.getGsmSignalStrength();


    }

}
Run Code Online (Sandbox Code Playgroud)

好的,但只有在它发生变化时才会运行.我需要当前的信号强度.

有没有办法只询问当前的信号强度?

小智 21

添加了CellSignalStrengthGsm()在API级别17中添加

CellSignalStrengthGsm().getDbm()将为您提供dBm的信号强度

 private static String getSignalStrength(Context context) throws SecurityException {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String strength = null;
    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) cellInfos.get(i);
                    CellSignalStrengthWcdma cellSignalStrengthWcdma = cellInfoWcdma.getCellSignalStrength();
                    strength = String.valueOf(cellSignalStrengthWcdma.getDbm());
                } else if (cellInfos.get(i) instanceof CellInfoGsm) {
                    CellInfoGsm cellInfogsm = (CellInfoGsm) cellInfos.get(i);
                    CellSignalStrengthGsm cellSignalStrengthGsm = cellInfogsm.getCellSignalStrength();
                    strength = String.valueOf(cellSignalStrengthGsm.getDbm());
                } else if (cellInfos.get(i) instanceof CellInfoLte) {
                    CellInfoLte cellInfoLte = (CellInfoLte) cellInfos.get(i);
                    CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();
                    strength = String.valueOf(cellSignalStrengthLte.getDbm());
                } else if (cellInfos.get(i) instanceof CellInfoCdma) {
                    CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfos.get(i);
                    CellSignalStrengthCdma cellSignalStrengthCdma = cellInfoCdma.getCellSignalStrength();
                    strength = String.valueOf(cellSignalStrengthCdma.getDbm());
                }
            }
        }
    }
    return strength;
}
Run Code Online (Sandbox Code Playgroud)

您可以从以下网址了解更多信息:https: //developer.android.com/reference/android/telephony/CellInfo.html

CellInfoCdma,CellInfoGsm,CellInfoLte,CellInfoWcdma是CellInfo的子类.其中提供了与您的移动网络相关的所有信息.


Mic*_*hal 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();
Run Code Online (Sandbox Code Playgroud)

  • 只是一个抬头:似乎有些设备(看着你三星)没有正确实现getAllCellInfo()并将返回null. (7认同)
  • 您还需要将此权限代码添加到清单... <uses-permission android:name ="android.permission.ACCESS_COARSE_LOCATION"/> (3认同)
  • 是否有API级别8的等效代码? (2认同)