user283494
user283494

Reputation: 1907

Android: Checking if headphones are plugged in

How can I check if headphones are currently plugged in. I don't want a broadcastreceiver which informs me when they have been connected to the device. I need something like:

if(/*headphone is connected*/)
 ...

Upvotes: 11

Views: 9423

Answers (3)

Mohsin Bagwan
Mohsin Bagwan

Reputation: 343

Use this code snippet

AudioManager am1 = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Log.i("am1.isWiredHeadsetOn()", am1.isWiredHeadsetOn()+"");
Log.i("am1.isMusicActive()", am1.isMusicActive()+"");
Log.i("am1.isSpeakerphoneOn()", am1.isSpeakerphoneOn()+"");

Upvotes: 2

mobillness.com
mobillness.com

Reputation: 1

This seems to do the job at least on 1.6; not sure whether it's supported in later versions (a is an instance of AudioManager)

boolean headphones = (a.getRouting(a.getMode()) & AudioManager.ROUTE_HEADSET) == AudioManager.ROUTE_HEADSET;

Upvotes: 0

David Webb
David Webb

Reputation: 193696

It looks like you'll be interested in the isWiredHeadsetOn() method and isBluetoothA2dpOn() method of the AudioManager class.

However, the isWiredHeadsetOn() method is only available in Android 2.0 or later. (The isBluetoothA2dpOn() method has been available since Android 1.5.)

Upvotes: 14

Related Questions