Question

I would like to store the cell signal strength, and I do it like this:

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


    }

}

Okay but this only runs if it changes. I need the current signal strength.

Is there a method to just ask for the current signal strength?

Was it helpful?

Solution 2

There is getAllCellInfo() method in TelephonyManager added in API 17 that could be good solution. Example of use:

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();

OTHER TIPS

CellSignalStrengthGsm() is added Added in API level 17

CellSignalStrengthGsm().getDbm() will give you the signal strength as 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;
}

Please note that above code will return strength of the last cell in the list.

You can learn more from: https://developer.android.com/reference/android/telephony/CellInfo.html

CellInfoCdma, CellInfoGsm, CellInfoLte, CellInfoWcdma are the subclasses of CellInfo. Which gives all information related to your mobile network.

If you are using a SDK version > 28, you can use getSignalStrength() function like so:

telephonyManager.getSignalStrength().getLevel();

Here is a fully implemented function:

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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top