Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destroy object of PhoneStateListener class?

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()); 
 }
like image 802
Krzysztof Kubacki Avatar asked Apr 25 '12 16:04

Krzysztof Kubacki


2 Answers

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

like image 167
Dan S Avatar answered Sep 28 '22 22:09

Dan S


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.

like image 27
Kevin Coppock Avatar answered Sep 28 '22 23:09

Kevin Coppock