Stop the listener after the first update

I want to know the signal strength of my current tower of Android cell phones. After some research that I found, I have to use PhoneStateListener that listen for the update to get the value (a weird way to do this IMHO).

So, I want to receive the signal as soon as I receive it, and stop the listener. This is the code I'm using:

 // 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()); } } } 

I want to stop the listener as soon as I send the message setStrength(value)

Any idea?

thanks

+6
source share
1 answer

From docs :

To unregister a listener, pass a listener and set events to the argument LISTEN_NONE (0).

So add this to your listener:

 telMan.listen(myListener, PhoneStateListener.LISTEN_NONE); 
+10
source

Source: https://habr.com/ru/post/906242/


All Articles