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()); 
 }
Dan S
  • 9,139
  • 3
  • 37
  • 48

2 Answers2

17

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

Dan S
  • 9,139
  • 3
  • 37
  • 48
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.

Community
  • 1
  • 1
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274