How to destroy object of PhoneStateListener class?
Asked Answered
B

2

5

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()); 
 }
Badr answered 25/4, 2012 at 16:57 Comment(0)
H
17

In the documentation it states to pass the listener object and flag LISTEN_NONE to unregister a listener.

Herminiahermione answered 25/4, 2012 at 17:4 Comment(0)
A
10

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.

Affirmation answered 25/4, 2012 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.