0

我试图在我的 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。有什么我想念的吗?

4

1 回答 1

0

好吧,这就是我后来尝试的方式,效果很好。我必须使 CurStrgth.setText() 独立于 Button 和 PhoneStateListener 内部,并从 OnCreate() 方法调用侦听器。它工作正常,只要信号强度发生变化,就会更新 CurStrgth TextView。我的代码如下:

public class MainActivity extends Activity implements OnClickListener {


TextView CurStrgth;
MyPhoneStateListener MyListener;
TelephonyManager Tel;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CurStrgth = (TextView) findViewById(R.id.tvCurCellStrength);

    MyListener = new MyPhoneStateListener();

    Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);


}


@Override
protected void onPause() {
    super.onPause();
    Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
}

@Override
protected void onResume() {
    super.onResume();
    Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

private class MyPhoneStateListener extends PhoneStateListener {
    String gsmStrength = "";

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
            gsmStrength = String
                .valueOf(signalStrength.getGsmSignalStrength() * 2 - 113);
        CurStrgth.setText(MyListener.getStrength() + "dBm");
    }

    public String getStrength() {
        return gsmStrength;
    }

}
}

现在,我检查了只是调用

    CurStrgth.setText(MyListener.getStrength() + "dBm");

在按钮的 OnClick() 方法中,它只显示一次值,但如果我稍后按下按钮,它永远不会更新强度并继续显示初始值。

于 2013-07-08T15:30:24.890 回答