Android 电话 CellSignalStrength

Android Telephony CellSignalStrength

抱歉我的英语不好。

我想问一下 android 电话:CellSignalStrength

我有如下代码显示 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());

当我运行它显示结果是-93

所以我想要的是字符串形式的结果及其信息: SIGNAL_STRENGTH_GOOD SIGNAL_STRENGTH_GREAT SIGNAL_STRENGTH_MODERATE SIGNAL_STRENGTH_POOR

喜欢下图:

不是前面的数字-93

您应该使用 getLevel()

,而不是使用 return“信号强度为 dBm”的 getDbm()

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

所以你从 CellSignalStrength:

中得到了一个 int 值
CellSignalStrength.SIGNAL_STRENGTH_GOOD
CellSignalStrength.SIGNAL_STRENGTH_GREAT
CellSignalStrength.SIGNAL_STRENGTH_MODERATE
CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN
CellSignalStrength.SIGNAL_STRENGTH_POOR

如果您仍想获取字符串而不是 int,您可以使用

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