Android listens to Home key messages through Intent. ACTION_CLOSE_SYSTEM_DIALOGS

Source: Internet
Author: User

Android listens to Home key messages through Intent. ACTION_CLOSE_SYSTEM_DIALOGS

Android processes messages with four buttons at the bottom of the screen differently:

1. The search button message is received in onKeyDown or onKeyUp;

2. The menu button message is received in the onCreateOptionsMenu, onKeyDown, or onKeyUp method;

3. messages that return keys 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();    }
The processing of Home key messages cannot be received through onKeyDown or onKeyUp, and android does not provide proprietary methods to receive button messages. I personally estimate that the home key is a backdoor for processing app exception information, for example, after ANR, pressing other buttons is not better than pressing the Home button. Therefore, android does not provide a method for users to listen to the home button messages to provide a better user experience.

 

Note:There are a variety of methods to listen to the Home button message on the Internet, but this is the only method.

 

However, there are always some methods. Each time you click the Home button, an action is sent as Intent. ACTION_CLOSE_SYSTEM_DIALOGS broadcast, which is to disable 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, for instructions, see the instructions above the Class Name:

 

Import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter;/*** Home button listener class * instructions for use: * 1. initialize HomeListen * HomeListen homeListen = new HomeListen (this); * homeListen. setOnHomeBtnPressListener (new setOnHomeBtnPressListener () {* @ Override * public void onHomeBtnPress () {* // press the Home button to call back *} ** @ Override * public void onHom EBtnLongPress () {* // call back by pressing the Home button **}); ** 2. Start the HomeListen broadcast in the onResume method: * homeListen. start (); ** 3. Stop the HomeListen broadcast in the onPause method: * 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 (OnH OmeBtnPressLitener onHomeBtnPressListener) {mOnHomeBtnPressListener = onHomeBtnPressListener;} public void start () {mContext. registerReceiver (mHomeBtnReceiver, mHomeBtnIntentFilter);} public void stop () {mContext. unregisterReceiver (mHomeBtnReceiver);} class HomeBtnReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {receive (context, intent);} privat E 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! = MOnHomeBtnPressListener) {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 (); public void onHomeBtnLongPress ();} private Context mContext = null; private IntentFilter mHomeBtnIntentFilter = null; private OnHomeBtnPressLitener mOnHomeBtnPressListener = null; private HomeBtnReceiver mHomeBtnReceiver = null ;}

Make the following calls in the 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 initH OmeListen () {mHomeListen = new HomeListen (this); mHomeListen. listener (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 listens to the Home button

 

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.