首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android擒获Home键的方法

2012-08-13 
android捕获Home键的方法Level5以上(包含)中,Activity类中有如下方法:public void onAttachedToWindow ()S

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

?

热点排行