Intent.ACTION_HEADSET_PLUG is received when activity starts
<p>I am trying to pause music that is playing when the headset is unplugged. </p>

<p>I have created a BroadcastReceiver that listens for ACTION_HEADSET_PLUG intents and acts upon them when the <em>state</em> extra is 0 (for unplugged). My problem is that an ACTION_HEADSET_PLUG intent is received by my BroadcastReceiver whenever the activity is started. This is not the behavior that I would expect. I would expect the Intent to be fired only when the headset is plugged in or unplugged. </p>

<p>Is there a reason that the ACTION_HEADSET_PLUG Intent is caught immediately after registering a receiver with that IntentFilter? Is there a clear way that I can work with this issue? </p>

<p>I would assume that since the default music player implements similar functionality when the headset is unplugged that it would be possible. </p>

<p>What am I missing?</p>

<p>This is the registration code</p>

<pre><code>registerReceiver(new HeadsetConnectionReceiver(), 
                 new IntentFilter(Intent.ACTION_HEADSET_PLUG));
</code></pre>

<p>This is the definition of HeadsetConnectionReceiver</p>

<pre><code>public class HeadsetConnectionReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Log.w(TAG, "ACTION_HEADSET_PLUG Intent received");
    }

}
</code></pre>
