Search code examples
androidtelephonymanager

How to prevent CALL_STATE_IDLE detection on app starts?


The app listens for incoming call and then stops playing music. Then, I want music to restart after the call ends. But I am having a problem with CALL_STATE_IDLE as it gets detected on app starts so any call inside its method is being invoked on app starts.

My code look like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   ...
    listenForIncomingCall();
   ...
   }
    private void listenForIncomingCall() {
    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                //Incoming call: Pause music
                //stop playing music
            } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                //Not in call: Play music

            //a code placed here activates on app starts

            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                //A call is dialing, active or on hold
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };
    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (mgr != null)

    {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

How can I prevent this? How can I register some listener if not in onCreate?


Solution

  • I found a supstitute solution. Feel free to use it. In case someone has a better one, feel free to share it with the community.

    private void listenForIncomingCall() {
        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            boolean toTrack = false; //to prevent triggering in onCreate
    
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    //Incoming call: Pause music
                    doSomething();
                } else if (state == TelephonyManager.CALL_STATE_IDLE) {
                    //Not in call: Play music
                    if (toTrack) {
                        doSomething();
                    }
                    toTrack = true;
                } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    //A call is dialing, active or on hold
                    if (toTrack) {
                        doSomething();
                    }
                    toTrack = true;
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (mgr != null)
    
        {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }