Search code examples
javaandroidandroid-homebutton

Disabling 'home' button in android for pattern lock app


I have developed a pattern lock android application. It is working fine.

In home screen, pattern is displayed. User could get through the lock only after unlocking with correct pattern(just like normal pattern lock).But the problem here is ,when the user presses 'home' button,the app closes.

I disabled back button.But couldn't find anywhere to disable 'home' button.

Could anyone provide me a solution?


Solution

  • Try this code

    Override the below method in your Activity,

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

    And handle the key event

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       if(keyCode == KeyEvent.KEYCODE_HOME)
        {
         Log.i("Home Button","Clicked Home button");
        }
     return false;
    }