15

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

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


    }

}

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

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

4

3 回答 3

27

添加了 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;
}

请注意,上面的代码将返回strength列表中的最后一个单元格。

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

CellInfoCdma、CellInfoGsm、CellInfoLte、CellInfoWcdma 是 CellInfo 的子类。它提供了与您的移动网络相关的所有信息。

于 2016-07-04T03:08:33.270 回答
22

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();
于 2013-05-13T21:59:18.527 回答
2

如果您使用的 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;
    }
}
于 2021-01-07T08:13:36.947 回答