How to destroy an object of class PhoneStateListener?

In some cases, I do not want to listen to the status of my phone. How to destroy an object of class PhoneStateListener?

I create an 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()); 
 }
+3
source share
2 answers

The documentation says to pass the listener object and the LISTEN_NONE flag to unregister the listener.

+12
source

Per this answer , you should save the link to TelephonyManagerand WnetPlayerPhoneCallListenerand disable it, for example:

mTm.listen(phoneCallListener, PhoneStateListener.LISTEN_NONE);

Why they don’t have standard methods addListener()and removeListener(), I don’t know, but this seems to be an acceptable method of solving your problem.

+7

All Articles