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:
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/
Whats a singleton? This produces an error for me.
Sorry that’s my fault, it’s a class relevant to my application and isn’t needed by yours at all
I’ve deleted it from the example
p.s. sorry the code layout is sooo poor, I need to find a plugin that will format it correctly.
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
Well this code should be able to register the head set button press but you will need to alter the code to work with your specific purpose.
Thanks matt,
and ya matt,this might be annoying to you but i m new to this android development,so giving highest priority to the media button event for my application,will it not hinder the process of receiving calls through that button i guess it has the highest priority right?
Thanks in advance
Sandesh I can’t remember the exact solution to your problem, but I did have an issue with the Music application stealing the button press OR it received the button press and then passed it down to my application (So both apps responded to the button press).
By setting the priority to high, it means your application is more likely to get the button press first, at which point you can consume the event and not allow it to pass down, which is what the abortBroadcast() method does.
If you really need this behaviour you should ensure you unregister the broadcast receiver (Look in the docs for unregisterReceiver()), this will then allow the system apps to have the usual, expected behaviour when your application dies or is in the background.
Thanks Matt,It worked
Good stuff, glad I could be of help
Happy Hacking
Hi Matt,
I am stuck with another problem.When I start a new activity from a broadcast receiver for example i
1.from the above code when the headsethook is pressed I call a new class which creates an intent and I start that acticity using startActivity(intent);
But when I do this I get an Null pointer Exception.But when I run the same activity by creating an new application it works fine,when i embed that code with the broadcast Receiver i get an exception at startActivity.
So is there some other way from which i can a new activity from the broadcast Receiver.
Sorry for bothering u,but any suggestion would be a great help.
Thanks in Advance
Sorry Sandesh, without seeing a little more code it’s hard to see what may be going wrong. But it seems like either the intent or the context is null.
Thanks matt for the reply,it worked for me. I had to set a flag like this
Intent startIntent = new Intent(context,CallNumber.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
on HeadsetHook pressed broadcast.
Hi Matt,
How can I record two clicks of headset button press?Do we have to time the headset button press if yes how to do it in onReceive method?
Cos based on the two clicks i have to start my activity.Thanks In Advance
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
Hi Rory,
Since so many people are having an issue with this, I’ll set up a simple example repository on github and get it working.
In terms of API version, I originally used this for a 2.1 project which I tested on a Nexus One with the supplied headset.
Some difference’s may occur for other devices and headsets.
Cheers,
Matt
Cool, thanks so much.
[...] 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 [...]