Android的 - 看看home键是pressedAndroid、home、pressed

2023-09-04 04:13:55 作者:Wasted(蹉跎)

我在做一个游戏,如果活动离开以任何方式由用户(背部或home键pressed),该活动需要张贴到脚本和结束活动结束比赛。

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 activity.

我可以检测,如果返回键是pressed,但是,我找不到检测如果home键是pssed $ P $任何有效的方法。我不能就这样结束的比赛中Activity_Pause方法,因为我们说用户会收到一个电话游戏中期。

I can detect if the back key is pressed, however, I cannot find any valid method to detect if the home key is pressed. I can't just end the game in the Activity_Pause method because let's say the user receives a phone call mid-game.

我知道你不能捕获的事件,但是,有没有人找到了一种方法,看看活动留下用户,而不是别的东西就像一个电话把它发送到后台。

I understand you can't trap the event, however, has anyone found a way to see if the activity was left by the user instead of something else like a phone call sending it to the background.

推荐答案

确定这里是变通,如果你坚持。 Android的下一个版本可能只是堵塞漏洞。

Ok here is the work around if you insist. Android next version may just close the loophole.

boolean mKeyPress;  
boolean mUserLeaveHint;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    mKeyPress = true;
    return super.onKeyDown(keyCode, event);
} 

@Override
protected void onUserLeaveHint()
{
    super.onUserLeaveHint();
    mUserLeaveHint = true;
}

@Override
protected void onPause()
{
    super.onPause();
    if (!mKeyPress && mUserLeaveHint)
    {
        // HOME_KEY is pressed
    }
}