2012-04-25 123 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