Raz
Raz

Reputation: 160

Using PhoneStateListener for setting the speaker on

I'm using PhoneStateListener so when the user receives a call or starts a call the speaker will be on instantly.

    PhoneStateListener phoneStateListener = new PhoneStateListener()
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber)
        {
            if (state == TelephonyManager.CALL_STATE_RINGING) 
            {


            } else if(state == TelephonyManager.CALL_STATE_IDLE)
            {

                AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

                audioManager.setSpeakerphoneOn(false);

            } else if(state == TelephonyManager.CALL_STATE_OFFHOOK)
            {

                AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                audioManager.setSpeakerphoneOn(true);
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };

    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

    if(mgr != null) 
    {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

This is the code I use. It worked fine until i've installed jelly bean, now I noticed that it sets the speaker on only when the user receives a call, but if he starts a call it does nothing

Upvotes: 0

Views: 930

Answers (1)

Timmetje
Timmetje

Reputation: 7694

Found it.

TelephonyManager seem to only work for incoming calls.

For outgoing calls create a broadcast listener with an intent android.intent.action.NEW_OUTGOING_CALL string parametrer for the IntentFilter and don't forget to give permission in AndroidMenifest to PROCESS_OUTGOING_CALLS. This will work. Whenever there is an outgoing call the onReceive will be called in the broadcast listener.

Upvotes: 1

Related Questions