我想知道我目前在Android上的手机信号塔的信号强度。经过一些研究后我发现,我应该使用PhoneStateListener
来监听更新以获取值(陌生的方式来做恕我直言)。
所以我想在得到信号后立即获取信号,然后停止听众。这是我使用的代码:
// object containing the information about the cell
MyGSMCell myGSMCell = new MyGSMCell(cid,lac);
// object listening for the signal strength
new GetGsmSignalStrength(myGSMCell, context);
...
public class GetGsmSignalStrength {
private MyGSMCell callback;
private TelephonyManager telMan;
private MyPhoneStateListener myListener;
public GetGsmSignalStrength(MyGSMCell callback, Context context) {
this.callback = callback;
this.myListener = new MyPhoneStateListener();
this.telMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
this.telMan.listen(this.myListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
private class MyPhoneStateListener extends PhoneStateListener
{
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
// get the strength
super.onSignalStrengthsChanged(signalStrength);
// return it to the MyGSMCell object to set the value
callback.setStrength(signalStrength.getGsmSignalStrength());
}
}
}
我想在发送setStrength(value)
有什么想法吗?
谢谢
答案 0 :(得分:10)
来自docs:
要取消注册侦听器,请传递侦听器对象并设置事件 LISTEN_NONE(0)的参数。
所以,将它添加到你的监听器:
telMan.listen(myListener, PhoneStateListener.LISTEN_NONE);