Droid Incredible Headphones Detection

2019-08-30 09:01发布

问题:

I'm developing an Android application for the Droid Incredible. When I plug in my headphones, an icon appears on the status bar, so I presume the phone must know headphones are present.

My code produces beeps in response to various user inputs, but I discovered today that's a REALLY BAD idea when the user is wearing headphones. Ow.

Does anybody have suggestions for how I can detect the presence of headphones programmatically, in Android??

Thanks, R.

回答1:

I found the AudioManager class on the developer site, it looks to have a helpful method for this, but I have not tested it:

AudioManager am = getSystemService(Context.AUDIO_SERVICE);
bool headsetEnabled = am.isWiredHeadsetOn();


回答2:

You find out when it changes with http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG

I'm not sure if it's possible to find out if your app is started after the headphones are plugged in/plugged out.