0

I'm trying to make custom lock screen. So, there I need not to allow user to press Home Button. In the beginning 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?

Razib
  • 10,965
  • 11
  • 53
  • 80
Zhambul
  • 942
  • 9
  • 23

1 Answers1

0

There is no "good" way to handle the home button to "do nothing". You really only have two options which may or may not be good enough for you:

  1. Create your own home launcher. Users must select your app as their home launcher.
  2. You could also create a Service where you add a view to your window and it gets plastered on top of everything else. See my previous answer here with "displaying" a webview in a Service: Android: Using WebView outside an Activity context. You will need to modify params to fill the screen and also fix WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE to use the right flags to allow clicks. This won't actually block the home button but it will basically disable it's functionality. Lastly, this might not work on Android 5.0+ as the Status bar's dropdown may overlay your window.
Community
  • 1
  • 1
Randy
  • 4,351
  • 2
  • 25
  • 46