`

Android 接受Home key Event

阅读更多
http://stackoverflow.com/questions/3898876/how-to-disable-the-home-key

This solution works from android 2.1,

Okay, this was supposed to be a hard question. But here is a way to crack it.

Override the below method in your Activity,

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And now handle the key event like this,

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

   if(keyCode == KeyEvent.KEYCODE_HOME)
    {
     Log.i("Home Button","Clicked");
    }
   if(keyCode==KeyEvent.KEYCODE_BACK)
   {

        finish();
   }
return false;
};
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics