27

I can't seem to find a way to know on android if the headphones are plugged in. I found various solutions but they always seem to return false. The only thing that seems to work properly is a BroadcastReceiver, but that's not what I need:

I just need something like this

if(headphones plugged in) {
}

Is there such a function? Does it require some special permissions?

dosse91214
  • 561
  • 1
  • 5
  • 17
  • http://stackoverflow.com/questions/6249023/detecting-whether-a-headset-is-plugged-into-an-android-device-or-not – ashish.n Dec 23 '14 at 12:22
  • http://stackoverflow.com/questions/13610258/how-to-detect-when-a-user-plugs-headset-on-android-device-opposite-of-action-a – ashish.n Dec 23 '14 at 12:22

3 Answers3

42

You can use this code for checking if the headset is plugged in

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.isWiredHeadsetOn();

(Don't worry about the deprecation, it's still usable for ONLY checking if the headset are plugged in.)

And you need <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

Available in Android 2.0 +

soshial
  • 5,906
  • 6
  • 32
  • 40
Naskov
  • 4,121
  • 5
  • 38
  • 62
  • the permission. that must be why it didn't work the first time i tried that solution. is there a way to do that without involving Context? – dosse91214 May 06 '13 at 08:55
  • @dosse91214 what is wrong with the permission? If you don't add the permission the isWiredHeadsetOn() always will return false. – Naskov May 06 '13 at 08:56
  • yes that's what i meant. the first time i tried this solution it didn't mention that i had to add the permission. thanks :D – dosse91214 May 06 '13 at 08:57
  • Great except that this method was deprecated in API 14 - http://developer.android.com/reference/android/media/AudioManager.html#isWiredHeadsetOn() – Phil Haigh Sep 30 '13 at 19:28
  • @Phil under the deprecation info it says `Use only to check is a headset is connected or not.` and that's exactly for what he wants to use it. – maid450 Apr 05 '14 at 19:57
  • @maid450 as Sood said, "So I guess it is okay to keep using it to check whether or not a wired headset is connected, but not to check whether or not audio is being routed to it or played over it.". – Naskov Apr 07 '14 at 08:45
  • @maid450 Point taken but it is bad engineering practice to write new code to use a deprecated API when there are non-deprecated solutions available. – Phil Haigh Apr 08 '14 at 10:44
  • @Naskov I think not. If you want the credit, you need to post your solution, not mine. – Phil Haigh Apr 09 '14 at 14:47
  • 2
    @Phil Stackoverflow is not about who will take the credits, it's about helping other people and sharing knowledge. I don't care about the rating. Feel free to post that as an answer instead of telling me the solution and I will ping the owner of this thread to accept your answer. – Naskov Apr 10 '14 at 14:59
  • @Naskov the asker has already identified the only reliable solution - using a BroadcastReceiver. – Phil Haigh Apr 14 '14 at 10:07
  • How would you do this with the new phones that require headphone converters that plug in through the usb port? This command and the one below always return false with that. – Elliptica Jan 02 '19 at 23:47
18

AudioManager.isWiredHeadsetOn() is DEPRECATED. So, you need to use AudioManager.getDevices() method instead:

private boolean isHeadphonesPlugged(){
        AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        AudioDeviceInfo[] audioDevices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
        for(AudioDeviceInfo deviceInfo : audioDevices){
            if(deviceInfo.getType()==AudioDeviceInfo.TYPE_WIRED_HEADPHONES
                    || deviceInfo.getType()==AudioDeviceInfo.TYPE_WIRED_HEADSET){
                return true;
            }
        }
        return false;
    }
valerybodak
  • 4,195
  • 2
  • 42
  • 53
0

audioManager.isWiredHeadsetOn() is deprecated as per below code from android.media.AudioManager

/**
 * Checks whether a wired headset is connected or not.
 * <p>This is not a valid indication that audio playback is
 * actually over the wired headset as audio routing depends on other conditions.
 *
 * @return true if a wired headset is connected.
 *         false if otherwise
 * @deprecated Use {@link AudioManager#getDevices(int)} instead to list available audio devices.
 */
public boolean isWiredHeadsetOn() {
    if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADSET,"")
            == AudioSystem.DEVICE_STATE_UNAVAILABLE &&
        AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADPHONE,"")
            == AudioSystem.DEVICE_STATE_UNAVAILABLE &&
        AudioSystem.getDeviceConnectionState(DEVICE_OUT_USB_HEADSET, "")
          == AudioSystem.DEVICE_STATE_UNAVAILABLE) {
        return false;
    } else {
        return true;
    }
}

so we need to use AudioManager#getDevices method like below

private boolean isWiredHeadsetOn(){
    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    AudioDeviceInfo[] audioDevices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
    for(AudioDeviceInfo deviceInfo : audioDevices){
        if(deviceInfo.getType()==AudioDeviceInfo.TYPE_WIRED_HEADPHONES
                || deviceInfo.getType()==AudioDeviceInfo.TYPE_WIRED_HEADSET){
            return true;
        }
    }
    return false;
}
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
Akbar
  • 11
  • 1