5

brought here is the code for making a phone call from my Activity

public void makeAPhoneCallWithSpeakerOn()
{
  String uri = "tel:" + posted_by.trim() ;
  Intent intent = new Intent(Intent.ACTION_CALL);
  intent.setData(Uri.parse(uri));
  startActivity(intent); 
}

question is:

how can I make the phone call and turn the speaker on?

10X Elad

Elad Gelman
  • 1,182
  • 1
  • 13
  • 27

2 Answers2

6

Use an AudioManager to turn on the speakers and a CallStateListener for receiving the end of the call.

Force
  • 6,312
  • 7
  • 54
  • 85
  • So you're saying that i should listen to outgoing calls? If so how can I tell that they came from my application? – Elad Gelman Jan 09 '12 at 18:29
  • No, before you start the call you register the listener, then activate the speakerphones. After receiving `CALL_STATE_IDLE` you remove the listener and deactivate the speakerphones again. – Force Jan 09 '12 at 18:33
  • Will try and report back here – Elad Gelman Jan 09 '12 at 18:35
  • 5
    I looked [here](http://stackoverflow.com/questions/2663108/android-set-speakerphone-on-programmatically), and using the listener- I caught the off hook event and called: `AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); audioManager.setMode(AudioManager.MODE_IN_CALL); audioManager.setSpeakerphoneOn(true);` also added permission MODIFY_AUDIO_SETTINGS, not working yet. – Elad Gelman Jan 11 '12 at 21:56
  • Then I guess the `setSpeakerphoneOn` has to be called on STATE_RINGING, as it might have no effect on STATE_OFFHOOK – Force Jan 11 '12 at 22:08
  • It works, without setMode(). turns out APK wasn't installed successfully – Elad Gelman Jan 11 '12 at 22:11
  • Hi @EladGelman , Could you expand on your answer, I have a similar question and I am not sure what to do. http://stackoverflow.com/q/12995902/804503 – Ayrton Senna Oct 22 '12 at 22:24
-2

I found out that if I add the code in this following order works best for me

      audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
      audioManager.setMode(AudioManager.MODE_IN_CALL); 
      audioManager.setSpeakerphoneOn(true);

whereas the following not work for me if I setSpeakerphoneOn(true) at the first line:

       audioManager.setSpeakerphoneOn(true);
       audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
       audioManager.setMode(AudioManager.MODE_IN_CALL); 
ksu
  • 882
  • 1
  • 11
  • 17