Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android headphone detection

Tags:

java

android

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?

like image 535
Soatl Avatar asked Feb 21 '26 12:02

Soatl


2 Answers

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.

like image 175
Will Tate Avatar answered Feb 24 '26 03:02

Will Tate


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...
}
like image 29
jmbouffard Avatar answered Feb 24 '26 03:02

jmbouffard