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