Android - See if home key is pressed

前端 未结 2 1155
夕颜
夕颜 2021-01-27 09:27

I\'m making a game and if the activity is left in any way by the user (back or home key pressed), the activity needs to end the game by posting to a script and ending the activi

2条回答
  •  故里飘歌
    2021-01-27 10:16

    Looks like a duplicate of this one

    Android, How to receive home button click through broadcast receiver?

    @Override
    public boolean dispatchKeyEvent(KeyEvent keyevent) {
    
        if (keyevent.getKeyCode() == KeyEvent.KEYCODE_HOME) {
            //Do here what you want
            return true;
        }
        else
            return super.dispatchKeyEvent(event);
    }
    

提交回复
热议问题