Android is inconsistent with the four key message processing that is often used at the bottom of the screen:
1, the Search button message is received in onkeydown or onkeyup;
2. The message of the menu key is received in Oncreateoptionsmenu, onkeydown or OnKeyUp method;
3. Messages that return keystrokes can be received in the onbackpressed, onkeydown, or onkeyup methods.
@Override public boolean onKeyDown (int keycode, keyevent event) {switch (keycode) { Case keyevent.keycode_back:{} break; Case keyevent.keycode_menu:{} break; Case keyevent.keycode_search:{} break; default:{} break; } return Super.onkeydown (KeyCode, event); } @Override public Boolean onKeyUp (Int. KeyCode, KeyEvent event) {switch (keycode) {case Keyevent.keycode _back:{} break; Case keyevent.keycode_menu:{} break; Case keyevent.keycode_search:{} break; default:{} break; } return Super.onkeyup (KeyCode, event); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {return true;} @Override public void onbackpressed () {super.onbackpressed (); }
for the Home button message processing, can not be received through onkeydown, OnKeyUp, Android also does not provide a proprietary method to receive key messages, the personal estimate of the home button is an app exception information processing backdoor, such as ANR, Pressing the other button is no better than pressing the home button. So Android does not provide a way for users to listen to home button messages in order to provide a better user experience.
Note: There are a variety of ways to listen for home button messages on the Web, but only this is better.
But there is always the way, every time you click on the Home button will be issued a ACTION for Intent.action_close_system_dialogs broadcast, it is to shut down the system dialog broadcast, We are able to monitor the home button message by using it. I have defined a home button listening tool class myself. Code such as the following. Instructions for use refer to the instructions above the class name:
Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.content.intentfilter;/** * Home Button Listening class * Instructions for use: * 1, Initialize Homelisten * Homelisten homelisten = new Homelisten (this); * Homelisten.setonhomebtnpresslistener (new Setonhomebtnpresslistener () {* @Override * public void onhomebtnpress () {*// Press Home Button CALLBACK *} * * @Override * public void onhomebtnlongpress () {*//long Press home button callback *} *}); * * 2, start Homelisten broadcast in Onresume method: * Homelisten.start (); * * 3, in the OnPause method stop Homelisten Broadcast: * Homelisten.stop (); * */public class Homelisten {public Homelisten (context context) {Mcontext = Context;mhomebtnreceiver = new Homebtnreceiver (); mhomebtnintentfilter = new Intentfilter (intent.action_close_system_dialogs);} public void Setonhomebtnpresslistener (Onhomebtnpresslitener onhomebtnpresslistener) {Monhomebtnpresslistener = Onhomebtnpresslistener;} public void Start () {Mcontext.registerreceiver (mhomebtnreceiver, mhomebtnintentfilter);} public voidStop () {mcontext.unregisterreceiver (mhomebtnreceiver);} Class Homebtnreceiver extends broadcastreceiver{@Overridepublic void onreceive (context context, Intent Intent) {receive (context, intent);}} private void receive (context context, Intent Intent) {String action = intent.getaction (); if (Action.equals (intent.action _close_system_dialogs) {String reason = Intent.getstringextra ("reason"); if (reason! = null) {if (null! = Monhomebtnpres Slistener) {if (Reason.equals ("HomeKey")) {//Press the Home button monhomebtnpresslistener.onhomebtnpress ();} else if (reason.equals ("Recentapps")) {//Long press the home button monhomebtnpresslistener.onhomebtnlongpress ();}}}} public interface onhomebtnpresslitener{public void Onhomebtnpress ();p ublic void onhomebtnlongpress (); Private Context Mcontext = null;private intentfilter mhomebtnintentfilter = null;private Onhomebtnpresslitener Monhomebtnpresslistener = null;private Homebtnreceiver mhomebtnreceiver = null;}
You can do this in activity such as the following call:
public class Homelistenactivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate ) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_home_listen_layout); Inithomelisten (); } @Override protected void Onresume () { super.onresume (); Mhomelisten.start (); } @Override protected void OnPause () { super.onpause (); Mhomelisten.stop (); } private void Inithomelisten () { Mhomelisten = new Homelisten (this); Mhomelisten.setonhomebtnpresslistener (New Onhomebtnpresslitener () {@Overridepublic void onhomebtnpress () {Showtoast ("Press the home button! " );} @Overridepublic void Onhomebtnlongpress () {showtoast ("Long Press the home button!) " );}}); } private void Showtoast (String toastinfostr) { Toast.maketext (this, toastinfostr, Toast.length_long). Show ();
} private Homelisten mhomelisten = null;}
Demo Program: Android Monitor Home button
Android listens to home button messages via Intent.action_close_system_dialogs