Android monitor Home button

One: The difference between Home key monitoring and Back key monitoring in Android :
(1). In Android, when the Home button is pressed, by default, the Activity in the foreground is Stopped, that is, the Activity is set to the stopped state [onStop()] instead of the destroyed state [onDestory()]. If you start the Activity again, instead of calling the onCreate() method, the onSavedInstanceState method is called. It starts from onRestart()-onStart()-onResume().
(2) When the back key is pressed, it is different. The back key defaults to finish the activity in the foreground, that is, the activity state is onDestory, and when the activity is started again, it starts from onCreate, and the onSavedInstanceState method will not be called.
 To summarize: pressing the Home button returns to the desktop, and back returns to the previous activity.

The implementation method of the Back key will not be repeated here, but the implementation method of the Home key will be mainly explained. Here are the references I found and thought about it myself. Please give advice. 

The implementation method of Home key monitoring:

(1). Register the broadcast in onResum, and cancel the broadcast in OnPause. (2). Intercept the Action Intent.ACTION_CLOSE_SYSTEM_DIALOGS in the broadcast, and determine whether to long press or click the Home button by obtaining the Reason field. code show as below:

(1). Home key monitoring package class:

package com.scd.homewatcher.util;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
 
/**
 * Home key monitoring package
 *
 * @author way
 *
 */
public class HomeWatcher {
 
  // callback interface
  public interface OnHomePressedListener {
 
    public void onHomePressed();
 
    public void onHomeLongPressed();
  }
 
  private static final String TAG = "HomeWatcher";
  /** context */
  private Context mContext;
  /** filter*/
  private IntentFilter mFilter;
  /** interface*/
  private OnHomePressedListener mListener;
  /** Broadcast receiver */
  private InnerRecevier mRecevier;
 
  public HomeWatcher(Context context) {
    mContext = context;
    mRecevier = new InnerRecevier();
    mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
  }
 
  /**
   * set monitor
   *
   * @param listener
   */
  public void setOnHomePressedListener(OnHomePressedListener listener) {
    mListener = listener;
  }
 
  /**
   * Start monitoring, register broadcast
   */
  public void startWatch() {
    if (mRecevier != null) {
      mContext.registerReceiver(mRecevier, mFilter);
    }
  }
 
  /**
   * Stop listening, log off the broadcast
   */
  public void stopWatch() {
    if (mRecevier != null) {
      mContext.unregisterReceiver(mRecevier);
    }
  }
 
  /**
   * Broadcast receiver
   */
  private 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.i(TAG, "action:" + action + ",reason:" + reason);
          if (mListener != null) {
            if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
              // short press the home button
              mListener.onHomePressed();
            } else if (reason
                .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
              // long press the home button
              mListener.onHomeLongPressed();
            }
          }
        }
      }
    }
  }
}

  (2).MainActivity class:

package com.scd.homewatcher;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
 
import com.scd.homewatcher.util.HomeWatcher;
import com.scd.homewatcher.util.HomeWatcher.OnHomePressedListener;
 
public class MainActivity extends Activity implements OnHomePressedListener {
  private HomeWatcher mHomeWatcher;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView(R.layout.activity_main);
 
  }
 
  @Override
  protected void onResume() {
    mHomeWatcher = new HomeWatcher(this);
    mHomeWatcher.setOnHomePressedListener(this);
    // register broadcast
    mHomeWatcher.startWatch();
    super.onResume();
  }
 
  @Override
  protected void onPause() {
    mHomeWatcher.setOnHomePressedListener (null);
    // logout broadcast
    mHomeWatcher.stopWatch ();
    super.onPause();
  }
 
  @Override
  public void onHomePressed() {
    // EVERYTHING
    Toast.makeText(this, "Short press the Home button to implement your own logic", Toast.LENGTH_SHORT).show();
 
  }
 
  @Override
  public void onHomeLongPressed() {
    // EVERYTHING
    Toast.makeText(this, "Long press the Home button to implement your own logic", Toast.LENGTH_SHORT).show();
 
  }
 
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
      System.out.println("Pressed the back key onKeyDown()");
      return false;
    } else {
      return super.onKeyDown(keyCode, event);
    }
 
  }
 
  @Override
  public void onBackPressed() {
    // super.onBackPressed() will automatically call the finish() method to close
    super.onBackPressed();
  }
 
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326612080&siteId=291194637