Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Listener after first update

I want to know the signal strength of my current cell tower on Android. After some research I found, that I should use a PhoneStateListener which listen for an update to get the value (strange way to do that IMHO).

So I want to get the signal as soon as I get it and stop the listener after. This is the code I use :

// 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 would like to stop the listener as soon as I send a setStrength(value)

Any idea ?

Thank you

like image 472
Martin Trigaux Avatar asked Jan 17 '12 23:01

Martin Trigaux


People also ask

How to stop listener in Android Studio?

You just use removeListener(this).

What is the role of phonestatelistener class?

A listener class for monitoring changes in specific telephony states on the device, including service state, signal strength, message waiting indicator (voicemail), and others.


1 Answers

From the docs:

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

So, add this to your listener:

telMan.listen(myListener, PhoneStateListener.LISTEN_NONE);
like image 59
aromero Avatar answered Oct 01 '22 16:10

aromero