In the work, we sometimes deal with the logic of the headset keys, mainly divided into two categories, one is a short press, one is a long press.
There are two ways to monitor the headset's key events:
Method One: Register a key event to monitor media button
Import Android.app.activity;import Android.app.progressdialog;import Android.content.broadcastreceiver;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.media.audiomanager;import Android.os.bundle;import Android.util.Log; Import Android.view.keyevent;public class Mainactivity extends Activity {private Audiomanager maudiomanager;private ComponentName mcomponentname; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); Maudiomanager = (Audiomanager) getSystemService ( Context.audio_service); Audiomanager registers a Mediabutton object mcomponentname = new ComponentName (Getpackagename (), MediaButtonReceiver.class.getName ());} @Overrideprotected void Onresume () {maudiomanager.registermediabuttoneventreceiver (mcomponentname); Registerreceiver (Headsetreceiver, New Intentfilter (Intent.action_headset_plug)); super.onresumE ();} @Overrideprotected void OnPause () {//unregister maudiomanager.unregistermediabuttoneventreceiver (mcomponentname); Unregisterreceiver (Headsetreceiver); Super.onpause ();} @Overrideprotected void OnDestroy () {Maudiomanager = Null;mcomponentname = Null;super.ondestroy ();} Private final Broadcastreceiver headsetreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (Context Context, Intent Intent) {String action = intent.getaction (); if (Action.equals (Intent.action_headset_plug)) {//Phone Headset Pluggedif (Intent.getintextra ("state", 0) = = 1) {//Do SOMETHING//LOG.D (TAG, "Headphone detection: Insert");//toast.maketext ( Context, "Headphone detection: Insert", Toast.length_short). Show (); Maudiomanager.registermediabuttoneventreceiver (mcomponentname);// Phone Head Unplugged} else {//do SOMETHING//LOG.D (TAG, "headphone detect: Not plugged in"),//toast.maketext (context, "Headphone detect: No insert", Toast.length_short). Show (); Maudiomanager.unregistermediabuttoneventreceiver (Mcomponentname);}}};
mediabuttonreceiver.java
Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.util.log;import Android.view.keyevent;import Android.widget.toast;public class MediaButtonReceiver extends Broadcastreceiver {private static String TAG = "Mediabuttonreceiver"; @Overridepublic void OnReceive (context context, Intent Intent) {//get actionstring intentaction = Intent.getaction ();//Get KeyEvent object KeyEvent keyevent = (keyevent) Intent. Getparcelableextra (intent.extra_key_event); LOG.I (TAG, "Action---->" + intentaction + "KeyEvent----->" + keyevent.tostring ());//press/release button int keyaction = key Event.getaction (); if (Intent.ACTION_MEDIA_BUTTON.equals (intentaction) && (Keyevent.action_down = = Keyaction ) {//Get the key byte code int keycode = Keyevent.getkeycode ();///Long downtime = Keyevent.geteventtime ();//Get Key code KEYCODE//S Tringbuilder sb = new StringBuilder ()////These are possible key codes that print out the user pressed key//if (Keyevent.keycode_media_next = = keycode) {//sb.app End ("keycode_medIa_next ");//}//Note: When we press the Media_button middle button, the actual departure is keycode_headsethook instead of//Keycode_media_play_pauseif ( Keyevent.keycode_media_play_pause = = keycode) {//sb.append ("Keycode_media_play_pause");} if (Keyevent.keycode_headsethook = = keycode) {//sb.append ("Keycode_headsethook");} if (keyevent.keycode_media_previous = = keycode) {//sb.append ("keycode_media_previous");} if (keyevent.keycode_media_stop = = keycode) {//sb.append ("Keycode_media_stop");} Output clicked Key code//log.i (TAG, sb.tostring ());//toast.maketext (context, sb.tostring (), Toast.length_short). Show (); else if (keyevent.action_up = = keyaction) {log.i ("Chengjie", "AAA");}}}
Note that the definition in Androidmanifest.xml
<receiver android:name= "Com.lenovo.longexposure.MediaButtonReceiver" > <intent-filter android: priority= "2147483647" > <action android:name= "Android.intent.action.MEDIA_BUTTON"/> </ Intent-filter> </receiver>
Note that this method only listens for short-press events on the headset, and if you want to listen for long-press events, use method two.
Method Two: Direct monitoring onkeydown Method
@Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (Keyevent.keycode_headsethook = = keycode) {//press the headset key if (Event.getrepeatcount () = = 0) { //If long press, the Getrepeatcount value will always be larger//short press} else {//Long Press}}}
Android Monitor Headset key event