【发布时间】:2012-11-16 14:38:44
【问题描述】:
我正在开发一个具有以下必要条件的应用程序:如果设备中插入了耳机并且用户将其移除,我需要将所有流静音。为此,我需要收听AudioManager.ACTION_AUDIO_BECOMING_NOISY 广播。还行吧!这里没问题。
但是当用户再次插入耳机时,我需要取消设备静音。但是没有AudioManager.ACTION_AUDIO_BECOMING_NOISY对面广播。我不知道耳机何时重新插入。
一种解决方案是定期查看AudioManager.isWiredHeadsetOn() 是否为true,但这对我来说似乎不是一个好的解决方案。
有没有办法检测用户何时将耳机插入设备?
已编辑:我尝试以这种方式使用Intent.ACTION_HEADSET_PLUG,但它不起作用。
在 manifest.xml 我放了:
<receiver android:name=".MusicIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
这是我MusicIntentReceiver.java的代码:
public class MusicIntentReceiver extends BroadcastReceiver {
public void onReceive(Context ctx, Intent intent) {
AudioManager audioManager = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
Log.d("Let's turn the sound on!");
//other things to un-mute the streams
}
}
}
还有其他解决方案可以尝试吗?
【问题讨论】:
-
您不能在清单中为 ACTION_HEADSET_PLUG 注册接收器。应该在代码中动态注册。developer.android.com/reference/android/media/…
标签: android broadcastreceiver android-audiomanager