noahnu noahnu - 1 year ago 173
Java Question

BroadcastReceiver for ACTION_MEDIA_BUTTON not working

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.

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.

The ACTION_MEDIA_BUTTON I want to handle is the single button on earphones that allow a user to pickup / end calls, play / pause music. Instead of my application handling this button, when I click it, the stock music player on my Nexus S Android starts playing a song.

I have not placed my code in another class, maybe this is why it's not working?

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):

IntentFilter mediaButtonFilter = new IntentFilter(
BroadcastReceiver brMediaButton = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d("Event", "Media button!");

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);

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.

In my manifest:

<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.MEDIA_BUTTON" />

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.

Like I said earlier, I have tried many variations of this. One example was the use of the code at this link broadcastreceiver onReceive problem ACTION_MEDIA_BUTTON Android with CommonsWare's corrections. Again, however, I modified it so it wasn't in a seperate class.

Thank you in advance for your help.

Answer Source

I tested this on a Samsung Galaxy S5 with Android 4.4.2. So what is important and what is not mentioned in other posts:

  • Register the receiver in the AndroidManifest.xml inside the application tag but outside from every activity tag.
  • Your receiver Broadcastreceiver need to be public and static
  • One activity need to register an MediaButtonEventReceiver to listen for the button presses

Okay and here some example code:

mAudioManager =  (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mReceiverComponent = new ComponentName(this,YourBroadcastReceiver.class);
// somewhere else

Here the receiver:

public static class YourBroadcastReceiver extends BroadcastReceiver{

    // Constructor is mandatory
    public MediaBroadcastReceiver ()
        super ();
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        Log.i (TAG_MEDIA, intentAction.toString() + " happended");
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            Log.i (TAG_MEDIA, "no media button information");
        KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            Log.i (TAG_MEDIA, "no keypress");
        // other stuff you want to do

And here the manifest snippet. If needed add priority for the intent-filter, but was not needed for me:

    <receiver android:name="OuterClass$YourBroadcastReceiver">
           <action android:name="android.intent.action.MEDIA_BUTTON" />
    <activity> ... </activity>

For the references: