Check whether headphones are plugged in

放肆的年华 提交于 2019-12-29 03:53:48

问题


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?


回答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 +




回答2:


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()+"");



回答3:


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;
    }



回答4:


Use Google Awareness API

It's very rich in providing contextual feeling.




回答5:


audioManager.isWiredHeadsetOn() is deprecated as per below code from android.media.AudioManager /** * Checks whether a wired headset is connected or not. *

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; }



来源:https://stackoverflow.com/questions/16395054/check-whether-headphones-are-plugged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!