30

I have a headset with single button and want to do a simple Toast when the button is pressed.

Right now I have the following code:

public class MediaButtonIntentReceiver extends BroadcastReceiver {

public MediaButtonIntentReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        return;
    }
    KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (event == null) {
        return;
    }
    int action = event.getAction();
    if (action == KeyEvent.ACTION_DOWN) {
    // do something
        Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); 
    }
    abortBroadcast();
}
}

And my main activity is the following:

public class mainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
    registerReceiver(r, filter); 

}
}

Nothing happens though when I push the button though.

I'm pretty sure something is wrong with my permissions/xml in the manifest. Here's the receiver XML so far:

    <receiver android:name=".MediaButtonIntentReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

....

and:

<uses-permission android:name="android.permission.BLUETOOTH" /> 

I notice in LogCat that when I press the button I get an error from "BluetoothIntentReceiver" saying "onReceive() Action : android.intent.action.MEDIA_BUTTON"

halfer
  • 19,824
  • 17
  • 99
  • 186
JDS
  • 16,388
  • 47
  • 161
  • 224

5 Answers5

33

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.

JDS
  • 16,388
  • 47
  • 161
  • 224
  • 1
    Thanks a lot for this post. But for me setting priority 1000 is not working so I have changed it to 10,000 then its works great. –  Jul 02 '11 at 08:14
  • I stumbled upon the same issue, and setting the priority to 10,000 solved it. Thanks. – gnclmorais Jan 12 '12 at 14:36
  • 3
    Correct, I haven't revisited this app for a very long time. But it does not work on newer Android versions; the original development was done back in the time of 2.2! EDIT - I believe though that Google has added an API to grab these headset button click inputs in a much easier fashion. Try searching around for that. – JDS Jul 04 '13 at 22:54
5

Here's what I've got that's working in Android 4.2.2

In my manifest.xml I do this:

<receiver android:name=".MediaButtonIntentReceiver">
<intent-filter>
    <action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>

NB: this is instead of calling registerReceiver.

In my Main Activity's onCreate I need to call the AudioManager:

((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(
    new ComponentName(
         getPackageName(), 
         MediaButtonIntentReceiever.class.getName()));

I have found it will work without the AudioManager call, but not for long!

noelicus
  • 14,468
  • 3
  • 92
  • 111
2

For a fully documented answer for Android >4.0 have a look here:

BroadcastReceiver for ACTION_MEDIA_BUTTON not working

Community
  • 1
  • 1
Rappel
  • 696
  • 2
  • 6
  • 14
2

You shouldn't use setPriority

You register your broadcast receiver in the manifest

You then register your broadcast receiver using:

AudioManager#registerMediaButtonEventReceiver

The argument to registerMediaButtonEventReceiver is a ComponentName that points your broadcast receiver.

pjulien
  • 1,369
  • 10
  • 14
0

If you don't want to use BroadcastReceiver, you can do this for Android >5.0 (API level 21 LOLLIPOP) using the MediaSession described here: https://stackoverflow.com/a/39413753/1386969

Community
  • 1
  • 1
lidox
  • 1,901
  • 3
  • 21
  • 40