Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

android - Check whether headphones are plugged in

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 +


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...