开发者

Android: Checking if headphones are plugged in

How can I check if headphones are currently plugged in. I don't want a broadcastreceiver which informs me when they have been connected to the device. I开发者_Go百科 need something like:

if(/*headphone is connected*/)
 ...


It looks like you'll be interested in the isWiredHeadsetOn() method and isBluetoothA2dpOn() method of the AudioManager class.

However, the isWiredHeadsetOn() method is only available in Android 2.0 or later. (The isBluetoothA2dpOn() method has been available since Android 1.5.)


Use this code snippet

AudioManager am1 = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Log.i("am1.isWiredHeadsetOn()", am1.isWiredHeadsetOn()+"");
Log.i("am1.isMusicActive()", am1.isMusicActive()+"");
Log.i("am1.isSpeakerphoneOn()", am1.isSpeakerphoneOn()+"");


This seems to do the job at least on 1.6; not sure whether it's supported in later versions (a is an instance of AudioManager)

boolean headphones = (a.getRouting(a.getMode()) & AudioManager.ROUTE_HEADSET) == AudioManager.ROUTE_HEADSET;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜