Android tutorial: check if headphones are plugged in

25 05 2013

Here is an another short, but useful tutorial showing how to check whether the headphones are plugged in or unpugged.

Check the headphones state

Object that knows about headphones state is AudioManager. You have to retrieve AudioManager from context and ask for headphones state like that:

public void handleHeadphonesState(Context context){
	AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

	if(am.isWiredHeadsetOn()) {
		// handle headphones plugged in
	} else{
		// handle headphones unplugged
	}
}

Deprecated?

The isWiredHeadsetOn() is deprecated since API level 14, but it is still OK to use it just to check whether headset is plugged in. It is NOT OK to use it to check whether the audio stream is being routed to it (see the documentation).

What else AudioManager knows?

Read the documentation here to find out details. Some operations it can perform are:

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

Advertisements

Actions

Information

4 responses

25 05 2013
ct2020

Really helpful. I’m trying to learn how to code for android so things like this are awesome! Thanks!

10 05 2014
MM

The method isWiredHeadsetOn() from the type AudioManager is deprecated!!!!!!!!

30 06 2014
Jacek Milewski

The isWiredHeadsetOn() is deprecated since API level 14, but it is still OK to use it just to check whether headset is plugged in. It is NOT OK to use it to check whether the audio stream is being routed to it. See http://developer.android.com/reference/android/media/AudioManager.html#isWiredHeadsetOn()

11 03 2015
Code 5 Group

Code 5 Group

Android tutorial: check if headphones are plugged in | Looks OK!

Give Your feedback:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: