android捕获Home键的方法

Level5以上(包含)中,Activity类中有如下方法:
public void onAttachedToWindow ()
Since: API Level 5

Called when the main window associated with the activity has been attached to the window manager. See View.onAttachedToWindow() for more information.

  1. private   boolean  catchHomeKey =  false ;  
  2.     @Override   
  3.         public   void  onAttachedToWindow() {  
  4.                 // TODO Auto-generated method stub   
  5.             if (catchHomeKey) {  
  6.                     this .getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);  
  7.             }  
  8.                 super .onAttachedToWindow();  
  9.         }  
  10.         @Override   
  11.         public   boolean  onKeyDown( int  keyCode, KeyEvent event) {  
  12.                 // TODO Auto-generated method stub   
  13.                 if (keyCode == KeyEvent.KEYCODE_HOME) {  
  14.                         Log.e(TAG, "Home key down" );  
  15.                 }  
  16.                 return   super .onKeyDown(keyCode, event);  
  17.         } 
  18. 重写Activity中的onAttachedToWindow方法,设置Type,就能捕获到Home键。
    当不需要捕获时,删除setType这一行就OK

 

你可能感兴趣的:(android)