-1

I am Using following method to disable HomeKey click event ,but after Clicking Home key My App will be closed. I want ,whenever user click home key my app will not be close.

@Override
protected void onUserLeaveHint() {
    super.onUserLeaveHint();
    moveTaskToBack(false);
}
Sagar Aghara
  • 640
  • 7
  • 19
ManiCR7
  • 1
  • 2

4 Answers4

1

Since Android 4 there is no effective method to deactivate the home button. That is the reason why we need another little hack. In general the idea is to detect when a new application is in foreground and restart your activity immediately.

Check this workaround: http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/

Jean Pimentel
  • 419
  • 5
  • 10
0

Override the below method in your Activity,

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And now handle the key event like this,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if(keyCode == KeyEvent.KEYCODE_HOME)
    {
     Log.i("Home Button","Clicked");
    }
   if(keyCode==KeyEvent.KEYCODE_BACK)
   {
        finish();
   }
 return false;
}
Ranjith KP
  • 858
  • 6
  • 18
  • 1
    "TYPE_KEYGUARD" shows error..but i'm using TYPE_KEYGUARD_DIALOG type...This error will be occur --- **java.lang.IllegalArgumentException: Window type can not be changed after the window is added**.-- – ManiCR7 Jan 20 '17 at 12:21
0

Put his into your activity's code. It will intercept back button click

  @Override
public void onBackPressed() {

}
Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34
0

u can ovverride home button pressed like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();                     
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Avinash Roy
  • 953
  • 1
  • 8
  • 25