title: BroadcastReceiver for ACTION_MEDIA_BUTTON not working
body: <p>I am writing an Android application for version 4.0.3 (ICS) of the Android OS. The issue is that I am not getting the output from my Log.d() in the onReceive() method of the BroadcastReceiver which means my application is not properly handling the broadcast.</p>
  
  <p>I have read countless questions about how to run code upon a ACTION_MEDIA_BUTTON being clicked. I have even copy + pasted code when mine did not work, just to see if it would work.</p>
  
  <p>The ACTION_MEDIA_BUTTON I want to handle is the <strong>single</strong> button on earphones that allow a user to pickup / end calls, play / pause music. <strong>Instead of my application handling this button, when I click it, the stock music player on my Nexus S Android starts playing a song</strong>.</p>
  
  <p>I have not placed my code in another class, maybe this is why it's not working?</p>
  
  <p>Here's the code found on the onCreate() method (this specific code I copied off a website after the code I wrote didn't work):</p>
  
  <pre><code>IntentFilter mediaButtonFilter = new IntentFilter(
              Intent.ACTION_MEDIA_BUTTON);
      mediaButtonFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
      BroadcastReceiver brMediaButton = new BroadcastReceiver() {
          public void onReceive(Context context, Intent intent) {
              Log.d("Event", "Media button!");
              this.abortBroadcast();
  
              KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
              if(key.getAction() == KeyEvent.ACTION_UP) {
                  int keycode = key.getKeyCode();
                  if(keycode == KeyEvent.KEYCODE_MEDIA_NEXT) {
                      Log.d("TestApp", "Next Pressed");
                  } else if(keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS) {
                      Log.d("TestApp", "Previous pressed");
                  } else if(keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                      Log.d("TestApp", "Head Set Hook pressed");
                  }
              }
  
          }
      };
      registerReceiver(brMediaButton, mediaButtonFilter);
  </code></pre>
  
  <p>All I really need to test for is the KEYCODE_HEADSETHOOK but it doesn't hurt to have the other code there for testing, I'll fix it up once I can get everything working correctly.</p>
  
  <p>In my manifest:</p>
  
  <pre><code>&lt;intent-filter android:priority="2147483647" &gt;
              &lt;action android:name="android.intent.action.MEDIA_BUTTON" /&gt;
  &lt;/intent-filter&gt;
  </code></pre>
  
  <p>I originally thought this may be a permissions issue since I didn't specify any permissions for this however I didn't receive any error message.</p>
  
  <p>Like I said earlier, I have tried many variations of this. One example was the use of the code at this link <a href="https://stackoverflow.com/questions/6603170/broadcastreceiver-onreceive-problem-action-media-button-android">broadcastreceiver onReceive problem ACTION_MEDIA_BUTTON Android</a> with CommonsWare's corrections. Again, however, I modified it so it wasn't in a seperate class.</p>
  
  <p>Thank you in advance for your help.</p>