Check whether headphones are plugged in
23,876
Solution 1
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 +
Solution 2
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;
}
Solution 3
Use this code...
AudioManager am1 = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Log.i("WiredHeadsetOn = ", am1.isWiredHeadsetOn()+"");
Log.i("MusicActive = ", am1.isMusicActive()+"");
Log.i("SpeakerphoneOn = ", am1.isSpeakerphoneOn()+"");
Related videos on Youtube
Comments
-
dosse91214 over 4 years
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?
-
ashish.n over 9 years
-
ashish.n over 9 years
-
-
dosse91214 about 11 yearsthe 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?
-
Naskov about 11 years@dosse91214 what is wrong with the permission? If you don't add the permission the isWiredHeadsetOn() always will return false.
-
dosse91214 about 11 yearsyes 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
-
Phil Haigh over 10 yearsGreat except that this method was deprecated in API 14 - developer.android.com/reference/android/media/…
-
maid450 about 10 years@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. -
Naskov about 10 years@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.".
-
Phil Haigh about 10 years@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 about 10 years@Naskov I think not. If you want the credit, you need to post your solution, not mine.
-
Naskov about 10 years@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.
-
Phil Haigh about 10 years@Naskov the asker has already identified the only reliable solution - using a BroadcastReceiver.
-
zionpi almost 8 yearsThe method isWiredHeadsetOn() from the type AudioManager is deprecated
-
Sasha Shpota over 5 yearsThis would work only staring from API level 23. Any idea how to use it for lover API levels, say 19?
-
Elliptica over 5 yearsHow 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.
-
David García Bodego over 4 yearsWelcome to SO! An answer with just code is useless, even if right. Try to explain a little bit what you attach