In some cases I don't want listen to state of my phone. How to destroy object of PhoneStateListener class?
I create object this way
try {
phoneCallListener = new WnetPlayerPhoneCallListener();
TelephonyManager mTM = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
Log.e("PhoneCallListener", "Exception: "+e.toString());
}
In the documentation it states to pass the listener object and flag LISTEN_NONE to unregister a listener.
Per this answer, you should keep a reference to TelephonyManager
and WnetPlayerPhoneCallListener
, and set it to disabled, like so:
mTm.listen(phoneCallListener, PhoneStateListener.LISTEN_NONE);
Why they don't just have standard addListener()
and removeListener()
methods, I don't know, but this seems to be the accepted method for solving your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With