get SignalStrength without Using PhoneStateListener onSignalStrengthchanged

11,274

On API level 17 only, here's some code that can be used in an Activity (or any other Context child class):

import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellSignalStrengthCdma;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.CellSignalStrengthLte;
import android.telephony.TelephonyManager;

try {
    final TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    for (final CellInfo info : tm.getAllCellInfo()) {
        if (info instanceof CellInfoGsm) {
            final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
            // do what you need
        } else if (info instanceof CellInfoCdma) {
            final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
            // do what you need
        } else if (info instanceof CellInfoLte) {
            final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
            // do what you need
        } else {
            throw new Exception("Unknown type of cell signal!");
        }
    }
} catch (Exception e) {
    Log.e(TAG, "Unable to obtain cell signal information", e);
}

Previous versions of Android require you to call the listener, there is no other alternative (see this link).

Also ensure that your application contains the appropriate permissions.

Share:
11,274
user2031865
Author by

user2031865

Updated on June 13, 2022

Comments

  • user2031865
    user2031865 almost 2 years

    does anyone know how to get the signal strength without having to call the onSignalStrengthChanged. The problem with onSignalStrengthchanged is that is it called when the signal strength changes and I need to get the value of signalstrength according to a different criteria.

    Thanks in advance.

  • user2031865
    user2031865 almost 11 years
    I only get 99 for signal strength and -1 for Bit error rate. It doesnt seem to give me the real value. do you know what could be wrong?
  • jitain sharma
    jitain sharma almost 10 years
    As per the GSM Signal Strength, valid values are (0-31, 99) as defined in TS 27.007 8.5 0 -113 dBm or less 1 -111 dBm 2...30 -109... -53 dBm 31 -51 dBm or greater 99 not known or not detectable see more over 3GPP TS 27.007