Gaunt Face | Matthew Gaunt

Using Android Headset Buttons (Earphone Buttons)

Hey’all

I needed to get access to the button presses on the earphone buttons that are built into the earphones that come with the Nexus One (Also with the G1 I think?) and I found it a tad harder than I expected, largely because I expected someone else of done it already for me.

So anyway, here we go in terms of how to get you up an running.

In your main activity you need to register a Broadcast Receiver:

1
2
3
4
HardButtonReceiver buttonReceiver = new HardButtonReceiver();
IntentFilter iF = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
iF.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
registerReceiver(buttonReceiver, iF);

We set the priority to high for the intent filter so we gain first dibs on the button press, otherwise the media player application might be the first application to detect the press and then drop the receiver, this will hence cause your broadcast receiver to appear as though it’s not working.

Now that we’ve set that up, we need something to catch the broadcasted intents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class HardButtonReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("TestApp", "Button press received");
        abortBroadcast();
       
        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");
            }
        }
    }
}

And that is all there is to it, for more information it might be worth looking at this thread:

http://groups.google.com/group/android-developers/browse_thread/thread/81e7fadd7f0b64fb/54d82fcdb0c01540

UPDATE: I’ve uploaded a full example on my github page, for details see: http://www.gauntface.co.uk/blog/2011/02/13/android-headset-full-example/



Fancy Leaving a Comment?

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


  1. Martin Thompson says:

    Whats a singleton? This produces an error for me.

    Reply

  2. sandesh indi says:

    Hi,
    I need to record the headset button press and use it for some other activity using the above intent Intent.ACTION_MEDIA_BUTTON .The think is based on the number of headset Key presses i want to perform an action in my application,So will the above code work for that too…..
    Thanks in advance

    Reply

  3. Rory Hewitt says:

    Hey Matt,

    I like the code, but I’m getting problems implementing it. Specifically, I get problems with registerReceiver() – Eclipse says it’s not recognized… Of course, I’m not a great Java developer at the best of times :)

    Do you have the entire source showing the various imports? Fair enough if you don’t want to be sending this out to others, but it would be v. helpful…

    Also, which version of the API/Android is this supported on?

    Thanks,

    Rory

    Reply

  4. [...] my Masters I ended up needing to use the headset of the Nexus One and from that project I uploaded a couple of code snippets. Within the comments someone asked for a full example of working code and I said I would put one [...]

    Reply