Skip to content Skip to sidebar Skip to footer

How To Catch The Home Button Click In Android?

From the answers in the stackoverflow I found that Home button can be overriden? But I found an application in the android market called 'Mxplayer' where you can lock all buttons w

Solution 1:

This solution works upto 2.3,

Override the below method in your Activity,

@OverridepublicvoidonAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And now handle the key event like this,

@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {

   if(keyCode == KeyEvent.KEYCODE_HOME)
    {
     Log.i("Home Button","Clicked");
    }
   if(keyCode==KeyEvent.KEYCODE_BACK)
   {

        finish();
   }
returnfalse;

};

Solution 2:

Yes you can. Use the following code:

@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        //do nothingreturntrue;
    }
    returnsuper.onKeyDown(keyCode, event);
}

Post a Comment for "How To Catch The Home Button Click In Android?"