2

即時通訊在我的活動中使用電話聽衆,但在完成我的活動後,用戶撥打電話,我的電話聽衆沒有死機,再次啓動活動!請幫幫我。Android:爲什麼在活動結束後PhoneCallListener仍然活着?

phoneListener = new PhoneCallListener(); 
telephonyManager = (TelephonyManager) 
      TransferActivity.this.getSystemService(Context.TELEPHONY_SERVICE); 
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

PhoneCallListener類:

private class PhoneCallListener extends PhoneStateListener { 
    boolean isPhoneCalling = false; 
    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 

     if (TelephonyManager.CALL_STATE_RINGING == state) { 
     } 
     if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
      isPhoneCalling = true; 
     } 
     if (TelephonyManager.CALL_STATE_IDLE == state) { 
      if (isPhoneCalling) { 
       isPhoneCalling = false; 
        Intent intent = getIntent(); 
        startActivity(intent); 
       } 
      } 
     } 
    } 
} 
+0

Intent intent = getIntent(); startActivity(intent); 你爲什麼要調用上面的代碼? – AAnkit 2012-07-26 10:13:19

+0

在你的'onPause()'方法中註銷你的監聽器。 – 2012-07-26 10:16:49

+0

謝謝,但如何? – Mehdico 2012-07-26 10:21:13

回答

5

你有沒有嘗試設置監聽器爲空,

telephonyManager.listen(null, PhoneStateListener.LISTEN_NONE); 
3

文檔說:

要取消註冊監聽器,通過監聽器對象並將events參數設置爲LISTEN_NONE(0)。

這裏是Doc的鏈接

相關問題