Android Home Key listening Encapsulation

Source: Internet
Author: User

As we all know, the following two methods cannot be used to listen to the Return key event: [java] view plaincopyprint?
@ Override
Public void onBackPressed (){
// Do something
// Super. onBackPressed ();
}

@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
// Do something
}
Return super. onKeyDown (keyCode, event );
}

@ Override
Public void onBackPressed (){
// Do something
// Super. onBackPressed ();
}
 
@ Override
Public 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:

 

[Java]
/**
* Home Key listening Encapsulation
*
* @ Author way
*
*/
Public class HomeWatcher {
 
Static final String TAG = "HomeWatcher ";
Private Context mContext;
Private IntentFilter mFilter;
Private OnHomePressedListener mListener;
Private InnerRecevier mRecevier;
 
// Callback Interface
Public interface OnHomePressedListener {
Public void onHomePressed ();
 
Public void onHomeLongPressed ();
}
 
Public HomeWatcher (Context context ){
MContext = context;
MFilter = new IntentFilter (Intent. ACTION_CLOSE_SYSTEM_DIALOGS );
}
 
/**
* Set a listener
*
* @ Param listener
*/
Public void setOnHomePressedListener (OnHomePressedListener listener ){
MListener = listener;
MRecevier = new InnerRecevier ();
}
 
/**
* Start listening and register Broadcast
*/
Public void startWatch (){
If (mRecevier! = Null ){
MContext. registerReceiver (mRecevier, mFilter );
}
}
 
/**
* Stop listening and log out of 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 SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions ";
Final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps ";
Final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey ";
 
@ Override
Public void onReceive (Context context, Intent intent ){
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 ();
}
}
}
}
}
}
}

/**
* Home Key listening Encapsulation
*
* @ Author way
*
*/
Public class HomeWatcher {

Static final String TAG = "HomeWatcher ";
Private Context mContext;
Private IntentFilter mFilter;
Private OnHomePressedListener mListener;
Private InnerRecevier mRecevier;

// Callback Interface
Public interface OnHomePressedListener {
Public void onHomePressed ();

Public void onHomeLongPressed ();
}

Public HomeWatcher (Context context ){
MContext = context;
MFilter = new IntentFilter (Intent. ACTION_CLOSE_SYSTEM_DIALOGS );
}

/**
* Set a listener
*
* @ Param listener
*/
Public void setOnHomePressedListener (OnHomePressedListener listener ){
MListener = listener;
MRecevier = new InnerRecevier ();
}

/**
* Start listening and register Broadcast
*/
Public void startWatch (){
If (mRecevier! = Null ){
MContext. registerReceiver (mRecevier, mFilter );
}
}

/**
* Stop listening and log out of 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 SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions ";
Final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps ";
Final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey ";

@ Override
Public void onReceive (Context context, Intent intent ){
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:


[Java]
/**
* HomeWatcher example
*
* @ Author way
*
*/
Public class MainActivity extends Activity {
Private static final String TAG = "MainActivity ";
Private HomeWatcher mHomeWatcher;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
 
MHomeWatcher = new HomeWatcher (this );
MHomeWatcher. setOnHomePressedListener (new OnHomePressedListener (){
@ Override
Public void onHomePressed (){
Log. e (TAG, "onHomePressed ");
}
 
@ Override
Public void onHomeLongPressed (){
Log. e (TAG, "onHomeLongPressed ");
}
});
MHomeWatcher. startWatch ();
}
 
@ Override
Protected void onPause (){
Super. onPause ();
MHomeWatcher. stopWatch (); // stop listening in onPause. Otherwise, an error is returned.
}
}

/**
* HomeWatcher example
*
* @ Author way
*
*/
Public class MainActivity extends Activity {
Private static final String TAG = "MainActivity ";
Private HomeWatcher mHomeWatcher;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

MHomeWatcher = new HomeWatcher (this );
MHomeWatcher. setOnHomePressedListener (new OnHomePressedListener (){
@ Override
Public void onHomePressed (){
Log. e (TAG, "onHomePressed ");
}

@ Override
Public void onHomeLongPressed (){
Log. e (TAG, "onHomeLongPressed ");
}
});
MHomeWatcher. startWatch ();
}

@ Override
Protected void onPause (){
Super. onPause ();
MHomeWatcher. stopWatch (); // stop listening in onPause. Otherwise, an error is returned.
}
}


 

Related Article

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.