block Home Button in Ice cream sandwich and jelly bean

Ronak Mehta picture Ronak Mehta · Sep 15, 2012 · Viewed 8.3k times · Source

I am developing Lock screen where i want to disable Home button in ice cream sandwich and in Jelly bean , i can block it using following methods in android 2.2 , 2.3

  @Override
public void onAttachedToWindow() {
    // TODO Auto-generate method stub
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

also tried this

   getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

Here i am also not getting event info via onPause Method or onKeyDown

But these methods done't work for me in ICS,Jelly bean if is there any method that can replace it then let me know

Answer

Swayam picture Swayam · Sep 24, 2012

Post ICS i.e. Android 4+, the overriding of the HomeButton has been removed for security reasons, to enable the user exit in case the application turns out to be a malware.

Plus, it is not a really good practice to not let the user navigate away from the application. But, since you are making a lock screen application, what you can do is declare the activity as a Launcher , so that when the HomeButton is pressed it will simply restart your application and remain there itself (the users would notice nothing but a slight flicker in the screen).

EDIT #1 : Here is another workaround, more suited to your needs.

EDIT #2 : Just came across this. Haven't tested it. But looks kinda promising. Not sure if it would work, but you could give it a try.