首页 > 代码库 > Android完美禁用Home键

Android完美禁用Home键

重写Activity的onAttachedToWindow 方法

@Override  public void onAttachedToWindow() {      System.out.println("Page01 -->onAttachedToWindow");      this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);      super.onAttachedToWindow();  }

监听OnKey事件

 @Override  public boolean onKeyDown(int keyCode, KeyEvent event) {      System.out.println("Page01 -->onKeyDown: keyCode: " + keyCode);      if (KeyEvent.KEYCODE_HOME == keyCode) {          System.out.println("HOME has been pressed yet ...");          // android.os.Process.killProcess(android.os.Process.myPid());          Toast.makeText(getApplicationContext(), "HOME 键已被禁用...",                  Toast.LENGTH_LONG).show();      }      return super.onKeyDown(keyCode, event); // 不会回到 home 页面  }

 

Android完美禁用Home键