簡體   English   中英

Android 電話 CellSignalStrength

[英]Android Telephony CellSignalStrength

對不起,我的英語不好。

我想問一下安卓電話: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() getDbm()不是使用返回“信號強度為 dBm”的 getDbm()

檢索整體信號質量的抽象級別值。 返回 SIGNAL_STRENGTH_NONE_OR_UNKNOWN 和 SIGNAL_STRENGTH_GREAT 之間的 int 值

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM