Android : why PhoneCallListener still alive after

2020-02-14 07:55发布

问题:

im using a phone call listener in my activity but after finishing my activity , afer user make a call, my phone call listener not dead and brig up activity again !! please help me.

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

PhoneCallListener class :

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

回答1:

The documentation says:

To un-register a listener, pass the listener object and set the events argument to PhoneStateListener#LISTEN_NONE (0)

Here is the link to the docs.



回答2:

Did you try setting the Listener to null as,

telephonyManager.listen(null,  PhoneStateListener.LISTEN_NONE);