Android listens to home button messages via Intent.action_close_system_dialogs

Source: Internet
Author: User

Android is inconsistent with the four key message processing that is commonly 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, personally estimated that the home button is an application of abnormal information processing of the backdoor, such as ANR, Pressing the other buttons is no better than pressing the home button, so Android does not provide a way for the user to listen to the home button message 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 can register it to listen to the home button message, I have customized a Home button monitoring tool class, the code is as follows, see instructions for use 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;}

Make the following calls in activity:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.