Method of Shielding Capturing and Modifying Home Key by Android Programming

  • 2021-07-26 08:53:57
  • OfStack

This paper describes the method of Home key shielding, capturing and modifying by Android programming. Share it for your reference, as follows:

Personal supplement: This method needs to add permission, and it intercepts home key after modification, so it is recommended to use it with caution ~

During the development process, I believe everyone has encountered troubles because they can't capture Home keys. Now they finally have a way. In Level5 above (included), Activity class has the following methods:


public void onAttachedToWindow ()

Since: API Level 5

Called when the main window associated with the activity has been attached to the window manager. See View.onAttachedToWindow() for more information.

See Also
* onAttachedToWindow()


private boolean catchHomeKey = false;
@Override
public void onAttachedToWindow() {
    // TODO Auto-generated method stub
  if(catchHomeKey) {
      this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
  }
    super.onAttachedToWindow();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(keyCode == KeyEvent.KEYCODE_HOME) {
        Log.e(TAG, "Home key down");
    }
    return super.onKeyDown(keyCode, event);
}

Override the onAttachedToWindow method in Activity and set Type to capture the Home key.

When no capture is needed, delete the line setType to OK.

For more readers interested in Android related contents, please check the topics on this site: "Summary of Android Database Operation Skills", "Summary of activity Operation Skills for Android Programming", "Summary of Android File Operation Skills", "Summary of SD Card Operation Methods for Android Programming Development", "Introduction and Advanced Tutorial for Android Development", "Summary of Android Resource Operation Skills", "Summary of Android View View Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: