2

I am developing android application with lock functionality. please suggest me how to disable all the hard keys programatically. here i am using beleow code to disable back button. i want like this functionality for all hard keys like home, menu button, Notification bar in android programmatically.

user2210356
  • 89
  • 2
  • 2
  • 9

3 Answers3

1

for back button (in activity or fragment)

@Override
    public void onBackPressed() {
}
Lahiru Prasanna
  • 822
  • 2
  • 14
  • 30
1
  @Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

switch( event.getKeyCode() ) {

     case KeyEvent.KEYCODE_MENU:
         result = true;
        break;

    case KeyEvent.KEYCODE_VOLUME_UP:
         result = true;
        break;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        result = true;
        break;
    case KeyEvent.KEYCODE_BACK:
        result = true;
        break;
     default:
        result= super.dispatchKeyEvent(event);
        break;
 }

 return result;
}

and check this also

Community
  • 1
  • 1
Vaishali Sutariya
  • 5,103
  • 29
  • 32
0

Override home button in android/ Disable home button in android/Stay on your activity while clicking home button in android

 @Override
    public void onAttachedToWindow(){ 
        Log.i("TESTE", "onAttachedToWindow");
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow(); 
    }
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            Log.i("TESTE", "BOTAO HOME");
            return true;
        }
        return super.onKeyDown(keyCode, event);   
    }
Sagar Limbani
  • 299
  • 1
  • 13