我试图在我的 3g 网络应用程序中显示当前信号强度和当前小区 ID 和 Lac。由于它必须与 API-8 兼容,因此我正在使用来自 android.telephony 的 SignalStrength 类。当我单击一个按钮时,现在我希望它显示当前单元格的 CID、Lac 和信号强度。我正在获取 CID 和 lac,但信号强度始终显示为 0。代码如下:
public void onClick(View v) {
switch (v.getId()) {
case R.id.bShowCell:
GsmCellLocation location;
String cellID = "";
String lac = "";
Context context = (Context) getApplicationContext();
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation) tm.getCellLocation();
cellID = String.valueOf(location.getCid());
lac = String.valueOf(location.getLac());
CurCell.setText(cellID);
CellLac.setText(lac);
CurStrgth.setText(getRSSI());
}
}
public String getRSSI() {
MyListener = new MyPhoneStateListener();
Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
String strength = MyListener.getStrength();
return strength;
}
class MyPhoneStateListener extends PhoneStateListener {
public int singalStrengths;
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
int asu = signalStrength.getGsmSignalStrength();
singalStrengths = -113 + 2 * asu;
}
public String getStrength() {
return String.valueOf(singalStrengths);
}
}
我在网上查了很多例子,我认为我的代码还可以。但是当我在调试模式下检查它时,我看到当我单击按钮时,程序永远不会进入 onSignalStrengthsChanged。有什么我想念的吗?