对不起我的英语不好。
我想问一下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早期的
发布于 2022-05-12 09:15:44
不要使用getDbm()来返回“信号强度作为dBm”,您应该使用getLevel()
检索整体信号质量的抽象级别值。在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);
}
}https://stackoverflow.com/questions/72212708
复制相似问题