-2

for my app i want to stop the screen off function

yes i just want to make it(Screen off function by pressing power button) disable that when user press power but and my app is running then screen must not off will give an toast message is it possible?

Goal :

  1. Disable auto screen timeout.

Done using this

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

used this post

now my first question is when my app will finish() time out will reset or screen will always on

2. Still using this user will be able to make screen off My second question is

Can i prevent user to do this and instead of screen get off an alert message is shown

Community
  • 1
  • 1
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95

1 Answers1

1

In your Activity just add the following snippet:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do nothing but show a warning message
        Toast.makeText(this, "you pressed the power button",Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

In case the above snippet doesn't work,you can try this then:

public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        //do nothing but show a warning message
        Toast.makeText(this, "you pressed the power button",Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.dispatchKeyEvent(event);
}
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108