0

I'm trying to make custom lock screen. So, there I need not to allow user to press Home Button. In the begining I write

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lock_screen);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        |WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

Then I override OnKeyDown

 @Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {

    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {
        //this is where I can do my stuff
        return true; //because I handled the event
    }
    if((keyCode == KeyEvent.KEYCODE_HOME)){

        return true;
    }

    return false;

}

Here I override onAttacheToWindow

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

But it's giving me error IllegalArgumentException: Window type can not be changed after the window is added. Where is my mistake?

How can I handle Home Button?

Zhambul
  • 942
  • 9
  • 23
  • **Window type can not be changed after the window is added** and you do this by calling `this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);` in `onAttachedToWindow()` method – Ibrahim Disouki Apr 12 '15 at 20:55
  • @hema18 How can I handle Home Button? – Zhambul Apr 12 '15 at 20:58
  • 1
    You can not handle home button but you can override `onStop()` method [Check this answer](http://stackoverflow.com/a/10869900/3512164) – Ibrahim Disouki Apr 12 '15 at 21:01

0 Answers0