Soatl
Soatl

Reputation: 10592

Android headphone detection

I have an app that will speak text messages to the user. I want to make it so that when the user hits a button called "Headphones on" the app will only speak to it when headphones are detected. Is there a command that will allow me to detect if headphones are plugged in or not?

Upvotes: 2

Views: 4702

Answers (2)

jmbouffard
jmbouffard

Reputation: 1615

It seems in this case you just want to check if headphone are connected before to start the audio playout, so you should use audioManager.isWiredHeadsetOn() like this:

AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isWiredHeadsetOn()) {
    // Play audio...
}

Upvotes: 4

Will Tate
Will Tate

Reputation: 33509

There is a broadcast made when the headphones are plugged in: http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG

You need to register a BroadcastReceiver to receive this and perform your required actions.

Upvotes: 5

Related Questions