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 页面
  }

 

posted @ 2014-09-02 16:20  有梦想的小人物  阅读(20097)  评论(0编辑  收藏  举报