Audio capture control button
I bought this Key Quick Button Pressy Dustproof Plug Jack Jack Jack Plug and I would like to develop applications for this.
How can I handle a click as a service or broadcast in Android in order to trigger some logic on click?
This android official blog doesn't work. I am using 4.0+
This is my code
Thank!
source to share
This will be what is called the Media Button, and it calls the system broadcast, which the BroadcastReceiver can handle, but needs to be implemented a little differently than normal receivers. Only one application at a time can receive a broadcast and the receiver must be registered with AudioManager
to activate. This developer page demonstrates how to implement and register a receiver. However, there are a few typos in this section. System service should be represented as AudioManager
, methods and calls registerMediaButtonEventReceiver()
, and unregisterMediaButtonEventReceiver()
must have an object ComponentName
as a parameter. For example:
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiverName = new ComponentName(mContext, RemoteControlReceiver.class);
...
// Start listening for button presses
am.registerMediaButtonEventReceiver(receiverName);
...
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(receiverName);
source to share