title: How do I intercept button presses on the headset in Android?
body: <p>I studied the <code>ACTION_MEDIA_BUTTON</code> intent and Im trying to use it and intercept the button presses and present them on screen using a toast.  I registered the receiver to intercept two intents:</p>
  
  <ol>
  <li><p><code>ACTION_HEADSET_PLUG</code> - plugging the headset</p></li>
  <li><p><code>ACTION_MEDIA_BUTTON</code>  - receiving the button presses</p></li>
  </ol>
  
  <p>This is done in my main activity:</p>
  
  <pre><code>        IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
          mediaFilter.setPriority(10000);
          registerReceiver(_receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
          registerReceiver(_receiver, mediaFilter);
  </code></pre>
  
  <p>This is the part of the receiver that handles the button presses:</p>
  
  <pre><code>    if (action.equals(Intent.ACTION_HEADSET_PLUG))
      {
          Toast.makeText(context, "earphones activity",Toast.LENGTH_SHORT).show();
          if (intent.getExtras().getInt("state")==1)//if plugged
              Toast.makeText(context, "earphones plugged",Toast.LENGTH_LONG).show();
          else Toast.makeText(context, "earphones un-plugged",Toast.LENGTH_LONG).show();
      }
      else 
      if (action.equals(Intent.ACTION_MEDIA_BUTTON))
      {
          Toast.makeText(context, "button pressed",Toast.LENGTH_LONG).show();
          key=intent.getExtras().getString("EXTRA_KEY_EVENT");
          Toast.makeText(context, key,Toast.LENGTH_LONG).show();
      }
  </code></pre>
  
  <p>Now the part that handles the headset plug-in and removal works fine, but the part that intercept the button press isn't.</p>
  
  <p>Is there any reason the code that handles the <code>ACTION_MEDIA_BUTTON</code> doesn't work?</p>
  
  <p>Is there a special permission I need to intercept such an intent?</p>
  
  <p>I'm using a Samsung Galaxy S2 to test the code.</p>
  
  <p>I've looked at all the similar posts and tried everything.  Unfortunately nothing seems to work.</p>