我想知道我目前在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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望一发一个就停止听众 setStrength(value)
任何的想法 ?
谢谢
aro*_*ero 10
来自文档:
要取消注册侦听器,请传递侦听器对象并将events参数设置为LISTEN_NONE(0).
所以,将它添加到你的监听器:
telMan.listen(myListener, PhoneStateListener.LISTEN_NONE);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3644 次 |
| 最近记录: |