Question

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);
                }
            }
        }
    }
}
Was it helpful?

Solution

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.

OTHER TIPS

Did you try setting the Listener to null as,

telephonyManager.listen(null,  PhoneStateListener.LISTEN_NONE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top