How to disable android hardware buttons programmatically?

前端 未结 3 1293
一个人的身影
一个人的身影 2021-01-16 07:51

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

相关标签:
3条回答
  • 2021-01-16 08:47

    for back button (in activity or fragment)

    @Override
        public void onBackPressed() {
    }
    
    0 讨论(0)
  • 2021-01-16 08:48

    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);   
        }
    
    0 讨论(0)
  • 2021-01-16 08:50
      @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

    0 讨论(0)
提交回复
热议问题