耳机插孔监听器的Android [英] Headphone Jack Listener Android
本文介绍了耳机插孔监听器的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道我可以检测设备上的耳机插孔是插在Android?我有一个音乐播放器,我需要暂停音乐时,耳机拔出。我已经发现的最接近的是使用 AudioManager
。那是正确的方向走?
Does anyone know how I can detect if the headphone jack on a device is unplugged on Android? I have a music player and I need to pause the music when the headphones are unplugged. The closest thing I have found is using the AudioManager
. Is that the right direction to go?
推荐答案
这是我落得这样做:
private class NoisyAudioStreamReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
pause();
}
}
}
private IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
private void startPlayback() {
registerReceiver(myNoisyAudioStreamReceiver(), intentFilter);
}
private void stopPlayback() {
unregisterReceiver(myNoisyAudioStreamReceiver);
}
我发现这个链接的回答:<一href="http://developer.android.com/training/managing-audio/audio-output.html">http://developer.android.com/training/managing-audio/audio-output.html
这篇关于耳机插孔监听器的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文