阅读完 Android 开发指南和此处的一些帖子后,我实现了一个简单的解决方案来捕获运行 Android 2.3.3 的平板电脑中的耳机拔出事件。
不幸的是,我似乎遗漏了一些东西,因为 onReceive() 事件永远不会被捕获。
下面是实现的代码:
public class MusicIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
Toast.makeText(context, "Headphones disconnected.", Toast.LENGTH_SHORT).show();
}
}
}
在 Android list 文件中:
<receiver android:name=".MusicIntentReceiver">
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
</receiver>
我错过了什么?
最佳答案
当音频输出源发生变化时,NOISY 过滤器会被触发,并可能导致奇怪的事情。
正确的耳机插头过滤器应该是:
IntentFilter commandFilter = new IntentFilter();
commandFilter.addAction(Intent.ACTION_HEADSET_PLUG);
list 应为:
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
关于android - ACTION_AUDIO_BECOMING_NOISY 未被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10160349/