0

How to make an app which will not listen to touch events and back/home pressing but will listen only to the power button. I've tried this but it wasn't successful.

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

        if (keyCode == KeyEvent.KEYCODE_POWER) {
            return true;
        }else if ((keyCode == KeyEvent.KEYCODE_HOME)|| (keyCode == KeyEvent.KEYCODE_BACK )){
            return false;
        }
        return false;
    }
Steve
  • 49
  • 1
  • 7
  • 2
    I'm pretty sure you're not supposed to do that and it is very highly discouraged as those buttons server as a "panic" button for most users. Not to mention that forcing them to restart just to get rid of your application would very much discourage them from using it to begin with. Unless the device is rooted, you can't do this. – Razgriz Dec 22 '15 at 01:56

3 Answers3

1

You could wrap you class with a custom View of yours and override its OnClickListener and OnTouchListener methods. Override them with blank methods.

Also, keep your code to set how your app should work when the Power Button is clicked.

For instance, if you wrap your layout with a RelativeLayout or a general View, you could use something like

relativeLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return false;
        }
});
relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            return false;
        }
});
rafaelc
  • 57,686
  • 15
  • 58
  • 82
1

To block user touch event try this solution. And if you want to disable back and home try to override this method of Activity:

@Override
public void onAttachedToWindow() {
     this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
     super.onAttachedToWindow();
}
Community
  • 1
  • 1
mr.icetea
  • 2,607
  • 3
  • 24
  • 42
1

three weeks ago, i want also to catch the home button, but i faied. i do some research, i find the android system don't support to modify it.you can visit this Overriding the Home button - how do I get rid of the choice?, about homebutton.

as for the touchEvent, i advice you can try this method ,override onTouchEvent (if you dont add onTouchListener, make it retrun fasle and do nothing, at the same time ,the activity also dont handle the onTounEvent or OnClick(whole layout).

Community
  • 1
  • 1
Lenoarod
  • 3,441
  • 14
  • 25