Registering a headset button click with BroadcastReceiver in Android

Just wanted to answer my own question in case others come across similar issues.

The code does work, just I wasn’t seeing the Toast because I had another headset button controller app installed (and running in the background), so I guess it took priority over mine. However when I put

    IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"
    MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
    filter.setPriority(1000); //this line sets receiver priority
    registerReceiver(r, filter);

It was able to work even with the other app installed. Also, you don’t need both the above AND the XML, one or the other is fine as ways of registering the intent receiver.

Leave a Comment