2012-04-25 138 views
3

在某些情况下,我不想听我的手机状态。 如何销毁PhoneStateListener类的对象?如何销毁PhoneStateListener类的对象?

我创建对象这样

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()); 
} 

回答

7

this answer,你应该保持到TelephonyManagerWnetPlayerPhoneCallListener参考,并将其设置为禁用,像这样:

mTm.listen(phoneCallListener, PhoneStateListener.LISTEN_NONE); 

为什么他们不这样做只是有标准的addListener()removeListener()方法,我不知道,但这似乎是解决您的问题的公认方法。

12

documentation中,它声明要传递侦听器对象和标志LISTEN_NONE以取消注册侦听器。

+0

非常感谢Dan S – 2012-04-26 10:42:06