android - ACTION_MEDIA_BUTTON Intent 的 BroadcastReceiver 在每次 MediaButton 单击时触发两次

标签 android bluetooth broadcastreceiver

我有一个简单但可能结构不当的 Android 应用程序。 它由两个 java 类组成:扩展 Activity 的 MainActivity 和扩展 BroadcastReceiver 的 RemoteControlReceiver。

我已按照以下两个链接中的说明设置 Mediabutton 接收器: http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html http://developer.android.com/training/managing-audio/volume-playback.html

问题是每当我按下蓝牙 Remote 上的媒体按钮(播放/暂停、下一首、上一首)时,broadcastReceiver 的 onReceive() 方法都会运行两次。或者更具体地说,整个 RemoteControlReceiver 被初始化为对象,对象的 onReceive() 方法运行,对象被废弃,然后重复。

我通过放置一个 static int mult = 0; 来对此进行测试;在主要 Activity 中。我在每次运行 onReceive 时将 mult 递增 1。每点击一次按钮它就会增加两次。

我不确定是什么导致它运行两次。硬件是每次点击发送两次信号,还是操作系统每次发送多个媒体按钮 Intent,或者我的广播接收器是否每次 Intent 运行两次?

我的 MediaButtonReceiver 代码:

public class RemoteControlReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())){
            KeyEvent Xevent = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            int keyType = Xevent.getKeyCode();



            Intent i = new Intent();
            i.setAction("com.MainActivity.Shakey.MEDIA_BUTTON");
            i.putExtra("keyType", keyType);
            context.sendBroadcast(i);
            Toast.makeText(context, String.valueOf(MainActivity.mult), Toast.LENGTH_SHORT).show();
            MainActivity.mult++;
            abortBroadcast();

        }

    }
}

这个接收者的过滤器在Manifest中注册如下:

<application>
 ...
   <receiver android:name=".RemoteControlReceiver">
       <intent-filter>
           <action android:name="android.intent.action.MEDIA_BUTTON"/>
       </intent-filter>
   </receiver>
 ...
</application>

The Broadcastreceiver is dynamically registered to the AudioManager object in the MainActivity's onResume(). And it is unregistered in onPause(). As the links said this is a sure way to get 1st priority on the media_button intents. I know I can ignore every even call of the broadcastReceiver by the use of a static variable. But I would like to know the cause of this issue.

附言 播放/暂停/上一个/下一个按钮适用于默认的安卓音乐播放器。

最佳答案

在我看来,问题在于 OnReceive 对按下和释放蓝牙按钮使用react。这就是为什么您的代码运行 2 次的原因。尝试这样做:

    if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
    KeyEvent Xevent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);    
    if ((KeyEvent.KEYCODE_MEDIA_PLAY == Xevent.getKeyCode()) && (Xevent.getAction() == KeyEvent.ACTION_DOWN)) {  //MainActivity.mult++; ...

或者不管你想做什么,你都可以使用 ACTION_UP。 我不确定,但我希望这对你有帮助。

关于android - ACTION_MEDIA_BUTTON Intent 的 BroadcastReceiver 在每次 MediaButton 单击时触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981662/

相关文章:

java - OpenCV 相机预览中未显示 mask

android - 如何禁用主要 Activity 的方向更改?

android - IntelliJ 没有为 Android 显示 'Test Module' 选项

android - 使用 ARC 进行自动化测试?

android - 在 Android 手机上启动 GATT 服务器。需要哪个 minSdk?

android - BroadcastReceiver onReceive() 不工作

ios - IOS 9.1/Xcode 7.1.1 中的蓝牙损坏

bluetooth - BLE 参数协商如何工作?

android - BOOT_COMPLETED 从未收到

java - android-BroadcastReceiver-java.lang.StackOverflowError