title: The BroadcastReceiver for the ACTION_MEDIA_BUTTON intent fires TWICE per MediaButton click
body: <p>I have a simple but probably badly structured Android application.
  It consists of two java classes: MainActivity extending Activity, and the RemoteControlReceiver that extends BroadcastReceiver.</p>
  
  <p>I have followed the instruction in the following two links to set up the Mediabutton receiver:
  <a href="http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html">http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html</a>
  <a href="http://developer.android.com/training/managing-audio/volume-playback.html">http://developer.android.com/training/managing-audio/volume-playback.html</a></p>
  
  <p>The issue is that whenever I press a media button (play/pause, next, previous) on my bluetooth remote, the broadcastReceiver's onReceive() method runs twice. Or more specifically the whole RemoteControlReceiver gets initialized into object, the object's onReceive() method runs, the object gets scrapped, and repeat.</p>
  
  <p>I tested for this by placing a static int mult = 0; in the MainActivity. I incremented the mult by 1 at every run of onReceive. And it increments twice per button click.</p>
  
  <p>I am not sure what causes it to run twice. Is the hardware sending double signal per click, or is the OS sending multiple media button intent per signal, or is my broadcastreceiver running twice per intent?</p>
  
  <p>My MediaButtonReceiver code:</p>
  
  <pre><code>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();
  
          }
  
      }
  }
  </code></pre>
  
  <p>This receiver's filter is registered in the Manifest as follows:</p>
  
  <blockquote>
  <pre><code>&lt;application&gt;
   ...
     &lt;receiver android:name=".RemoteControlReceiver"&gt;
         &lt;intent-filter&gt;
             &lt;action android:name="android.intent.action.MEDIA_BUTTON"/&gt;
         &lt;/intent-filter&gt;
     &lt;/receiver&gt;
   ...
  &lt;/application&gt;
  </code></pre>
    
    <p>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.</p>
  </blockquote>
  
  <p>PS
  The play/pause/previous/next buttons work fine for the default android music player.</p>