我编写了一段 Android 代码,它通过回调注册 aPhoneStateListener
并在发生变化时报告 GSM 信号强度值
。onSignalStrengthsChanged
一切正常,但我想在第一次更改发生之前获得初始信号强度。有没有办法在方法结束之前找出 GSM 信号强度onCreate
(参见代码末尾的注释行)?
这是我的代码:
public class MainActivity extends Activity {
private int mStrength;
private PhoneStateListener phoneStateListener;
private TelephonyManager telephonyManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneStateListener = new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
int currStrength = signalStrength.getGsmSignalStrength();
mStrength = currStrength;
Toast.makeText(getApplicationContext(),
"signal:" + String.valueOf(mStrength),
Toast.LENGTH_SHORT).show();
}
};
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
// What is value of GSM signal here? ...
// Log.d("SignalStrength:", "??");
}
}