Android headphone detection
I have an app that will speak text messages to the user. I want to make it so that when the user hits a button called "H开发者_JS百科eadphones on" the app will only speak to it when headphones are detected. Is there a command that will allow me to detect if headphones are plugged in or not?
There is a broadcast made when the headphones are plugged in: http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG
You need to register a BroadcastReceiver
to receive this and perform your required actions.
It seems in this case you just want to check if headphone are connected before to start the audio playout, so you should use audioManager.isWiredHeadsetOn() like this:
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isWiredHeadsetOn()) {
// Play audio...
}
精彩评论