Thursday 29 September 2011

Block Home Key

I searched a lot to block Home key but no success and finally I got it and here it is for you guys.

@Override
public void onAttachedToWindow() {

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();

}

Note: You should never block the Home key. Please see this post before doing so.

4 comments:

  1. It worked fine, but the activity is showing the notification bar which i don't want to.
    I have these 2 lines placed on (onCreate) method:
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    when I add your method; the notification bar appears.

    Kindly advice

    ReplyDelete
  2. Yes, it is the issue with this code. I have tried a lot but no luck. If you find something then do let me know.

    ReplyDelete
  3. @Vineet - Although I appreciate that you have taken time to share this but this must be considered a criminal offense in the android environment! Any app that enforces such constraints could be considered to be malware.

    ReplyDelete
  4. @ABHIJIT I agree with you and for that reason I have left a note with a post why Home key should not be intercepted.

    ReplyDelete