title: ACTION_HEADSET_PLUG slow broadcast response
body: <p>I want my music player to pause music when the  ear-phones are disconnected, and to resume music when the ear phones are re-connected. I got this set up to work with a broad cast receiver, but the problem is I can hear music playing from the speakers a split second after I disconnect the ear phones. It's like my receiver is not receiving the broad cast quickly enough? What's going on?</p>
  
  <p>Here I register my receiver in my <code>onCreate</code> method in my main activity class</p>
  
  <pre><code>MusicIntentReceiver receiver = new MusicIntentReceiver();
  registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
  </code></pre>
  
  <p>Here is my intent receiver class</p>
  
  <pre><code>public class MusicIntentReceiver extends android.content.BroadcastReceiver {
      @Override
      public void onReceive(Context ctx, Intent intent) {
  
          if (intent.getAction().equals(android.media.AudioManager.ACTION_HEADSET_PLUG)) {
  
              if(intent.getIntExtra("state",0)==0){ // 0 for unplugged (if it becomes unplugged)
  
                  //Pause music
              }
  
              if(intent.getIntExtra("state",0)==1){ // 1 for plugged (if it becomes plugged)
  
                  //Resume music
              }
          }
      }
  }
  </code></pre>