Android : why PhoneCallListener still alive after activity finish?

时光毁灭记忆、已成空白 提交于 2019-11-28 08:57:25

问题


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


来源:https://stackoverflow.com/questions/11666853/android-why-phonecalllistener-still-alive-after-activity-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!