Mathews Edward
Mathews Edward

Reputation: 75

Android Telephony CellSignalStrength

sorry for my bad english.

I want to ask about android telephony : CellSignalStrength

I have code like below to display signal strength information on android..

public class MainActivity extends AppCompatActivity  {

private TextView textView2;

public String gsmStrength;

@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   
    textView2 = (TextView) findViewById(R.id.textView2);

    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);



    try {

        for (CellInfo info : tm.getAllCellInfo()) {
            if (info instanceof CellInfoGsm) {
                CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
                // do what you need
                gsmStrength = String.valueOf(gsm.getDbm());
            } else if (info instanceof CellInfoCdma) {
                CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
                gsmStrength = String.valueOf(cdma.getDbm());
            } else if (info instanceof CellInfoLte) {
                CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
                gsmStrength = String.valueOf(lte.getDbm());
            } else {
                gsmStrength = String.valueOf("UNknown");
            }

        }


    }catch (Exception e){
        Log.d("SignalStrength", "+++++++++++++++++++++++++++++++ null array spot 3: " + e);
    }

    textView2.setText(gsmStrength.toString());

when I run it shows the result is -93

so what I want is the result in the form of a string with what information it is: SIGNAL_STRENGTH_GOOD SIGNAL_STRENGTH_GREAT SIGNAL_STRENGTH_MODERATE SIGNAL_STRENGTH_POOR

like the picture below: enter image description here

not the number -93 earlier

Upvotes: 0

Views: 1182

Answers (1)

Robert
Robert

Reputation: 42764

Instead of using getDbm() which return the "signal strength as dBm" you should use getLevel()

Retrieve an abstract level value for the overall signal quality. Returns int value between SIGNAL_STRENGTH_NONE_OR_UNKNOWN and SIGNAL_STRENGTH_GREAT inclusive

https://developer.android.com/reference/android/telephony/CellSignalStrengthGsm#getLevel()

So you get one of the int values from CellSignalStrength:

CellSignalStrength.SIGNAL_STRENGTH_GOOD
CellSignalStrength.SIGNAL_STRENGTH_GREAT
CellSignalStrength.SIGNAL_STRENGTH_MODERATE
CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN
CellSignalStrength.SIGNAL_STRENGTH_POOR

If you still want to get a string instead of the int you can use

public static String getLevelString(int level) {
    switch(level) {
        case CellSignalStrength.SIGNAL_STRENGTH_GOOD:
            return "SIGNAL_STRENGTH_GOOD";
        case CellSignalStrength.SIGNAL_STRENGTH_GREAT:
            return "SIGNAL_STRENGTH_GREAT";
        case CellSignalStrength.SIGNAL_STRENGTH_MODERATE:
            return "SIGNAL_STRENGTH_MODERATE";
        case CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN:
            return "SIGNAL_STRENGTH_NONE_OR_UNKNOWN";
        case CellSignalStrength.SIGNAL_STRENGTH_POOR:
            return "SIGNAL_STRENGTH_POOR";
        default:
            throw new RuntimeException("Unsupported level " + level);
    }
}

Upvotes: 2

Related Questions