0

I would like to use the Phone Speakers while a Headset is plugged-in, instead of the Headset-Speakers. enter image description here

Is it possible to do that? if yes, how can I achieve it?
I'm developing an App in Java for Android 9.
Detection if Headset is Plugged in works so far:

public class HeadsetIntentReceiver extends BroadcastReceiver {
private String TAG = "HeadSet";

public HeadsetIntentReceiver() {
    Log.d(TAG, "Created");
}

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
        int state = intent.getIntExtra("state", -1);
        switch(state) {
            case(0):
                Log.d(TAG, "Headset unplugged");
                break;
            case(1):
                Log.d(TAG, "Headset plugged");
                break;
            default:
                Log.d(TAG, "Error");
        }
    }
}
}

EDIT:
I tried the following Solutions, but Nothing happend:

  1. How to mute audio in headset but let it play on speaker programmatically?
  2. How to disable the wired headset programmatically in Java

    Thanks in Advance for any helpful advice.


(Please don't answer "Unplug the Headset")

Michael
  • 59
  • 1
  • 8

1 Answers1

0

I think you are looking for AudioManager

Attach AUDIO_SERVICE to AudioManager, set AudioManager as MODE_IN_COMMUNICATION and then set your speaker phone on. You can use the speaker as if you were talking on the phone.

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

Remember to restore it after use. for example,

protected void onPause() {
    super.onPause();
    audioManager.setMode(AudioManager.MODE_NORMAL);
    audioManager.setSpeakerphoneOn(false);
}
Fleta
  • 18
  • 3
  • Hi Fleta, unfortunately, the solution doesn't seem to work. I called myself with another phone while a working headset was on the phone. The sound only came from the headset :/ – Michael Dec 05 '19 at 09:01
  • Just in case it is necessary to know: The Call was made via VoIP. – Michael Dec 05 '19 at 09:11
  • Hi Michael, sorry for incorrect answer. Can you try again by changing the mode `MODE_IN_COMMUNICATION` to `MODE_CURRENT` or `MODE_IN_CALL` ? – Fleta Dec 05 '19 at 09:13
  • Hi Fleta, Nothing changed after trying These different modes :/. Still same behavior. – Michael Dec 05 '19 at 09:16
  • 2
    I discovered, that this Code must be executed, after the Call has been accepted instead of before. So basically, this answer is correct. – Michael Dec 10 '19 at 08:51
  • 1
    @Michael i am facing this weird problem making a phone call but the speaker is not turning on but how can we detect if an outgoing call is connected? – Mateen Chaudhry Jan 21 '20 at 06:43