Android : why PhoneCallListener still alive after

2020-02-14 08:01发布

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

2条回答
Lonely孤独者°
2楼-- · 2020-02-14 08:06

Did you try setting the Listener to null as,

telephonyManager.listen(null,  PhoneStateListener.LISTEN_NONE);
查看更多
3楼-- · 2020-02-14 08:31

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.

查看更多
登录 后发表回答