Check whether headphones are plugged in
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;
}
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 +