As we all know, the following two methods cannot be used to listen to the Return key event:
@Overridepublic void onBackPressed() {//do something//super.onBackPressed();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_BACK){//do something}return super.onKeyDown(keyCode, event);}
As a matter of course, the method for listening to the Home Key is: If (keycode = keyevent. keycode_home ). But in fact, this is not feasible, because at this time, the Home key message has been intercepted at the framework layer, so, in the application, the Home key message cannot be monitored through this method. So how can we capture and handle the Home key event? In fact, the system sends a broadcast to us. Next, we will share with you the encapsulated Method for listening to the Home key:
Homewatcher class:
/*** Home Key listening encapsulation ** @ author way **/public class homewatcher {static final string tag = "homewatcher"; private context mcontext; private intentfilter mfilter; private listener mlistener; private innerrecevier mrecevier; // callback interface public interface listener {public void onhomepressed (); Public void onhomelongpressed ();} public homewatcher (context) {mcontext = context; mfilter = New intentfilter (intent. action_close_system_dialogs);}/*** set listener ** @ Param listener */Public void listener (onhomepressedlistener listener) {mlistener = listener; mrecevier = new innerrecevier ();} /*** start listening and register broadcast */Public void startwatch () {If (mrecevier! = NULL) {mcontext. registerreceiver (mrecevier, mfilter) ;}}/*** stop listening, cancel broadcasting */Public void stopwatch () {If (mrecevier! = NULL) {mcontext. unregisterreceiver (mrecevier) ;}}/*** broadcast receiver */class innerrecevier extends broadcastreceiver {final string system_dialog_reason_key = "reason"; final string comment = "globalactions "; final string system_dialog_reason_recent_apps = "recentapps"; final string system_dialog_reason_home_key = "homekey"; @ overridepublic void onreceive (context, intent int Ent) {string action = intent. getaction (); If (action. Equals (intent. action_close_system_dialogs) {string reason = intent. getstringextra (system_dialog_reason_key); If (reason! = NULL) {log. E (TAG, "Action:" + Action + ", reason:" + reason); If (mlistener! = NULL) {If (reason. equals (system_dialog_reason_home_key) {// press the Home Key mlistener. onhomepressed ();} else if (reason. equals (system_dialog_reason_recent_apps) {// long press the Home Key mlistener. onhomelongpressed ();}}}}}}}
Next, we need to enable the listener in the application. The following uses the application in the activity as an example:
/*** Application homewatcher example ** @ author way **/public class mainactivity extends activity {Private Static final string tag = "mainactivity"; private homewatcher mhomewatcher; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mhomewatcher = new homewatcher (this); mhomewatcher. setonhomepressedlistener (New onhomepressedlisten Er () {@ overridepublic void onhomepressed () {log. E (TAG, "onhomepressed") ;}@ overridepublic void onhomelongpressed () {log. E (TAG, "onhomelongpressed") ;}}); mhomewatcher. startwatch () ;}@ overrideprotected void onpause () {super. onpause (); mhomewatcher. stopwatch (); // stop listening in onpause; otherwise, an error will be reported .}}