简体   繁体   English

Android:睡眠后需要恢复游戏

[英]Android : need to resume game after sleep

I want my game to be able to continue from where its left before phone went to sleep(user press power button). 我希望我的游戏能够从手机进入睡眠状态之前的左侧继续(用户按下电源按钮)。 My game work fine with home button. 使用主页按钮,我的游戏可以正常工作。 when user press home button and open game again, game start where it should be. 当用户按下主屏幕按钮并再次打开游戏时,游戏将从应有的位置开始。 But in the case user press power button(on google htc nexus one), it crashes. 但是在用户按下电源按钮的情况下(它会崩溃)。 But when I test the game on samsung galaxy tablet 10, it works fine. 但是,当我在三星银河平板电脑10上测试游戏时,它运行正常。

In LogCat , I could observe that when the user presses power button on galaxy tablet, onPause() is called. LogCat ,我可以观察到,当用户按下星系平板电脑上的电源按钮时,将调用onPause() and when user press again onResume() is called which is normal. 当用户再次按下onResume()时 ,这是正常的。

But on google nexus, when the user press power button, onPause(), onResume(),onPause() is called respectively before it went into sleep. 但是在Google Nexus上,当用户按下电源按钮时,分别在进入睡眠状态之前分别调用onPause(),onResume(),onPause() And nothing gets called when the user press power button again. 当用户再次按下电源按钮时,没有任何声音被调用。

Please advice how can I fix the problem. 请提出建议,我该如何解决该问题。 Thanks in advance 提前致谢

EDIT 编辑

Looks like this problem is observed only on device that have portrait orientation by default. 看起来此问题仅在默认情况下具有纵向的设备上观察到。

I manage to fix the problem by following answer to this question. 我设法通过遵循该问题的答案来解决问题。 After applying this solution, system did not crash any more. 应用此解决方案后,系统不再崩溃。 but after system wake, width and height was swapped. 但是系统唤醒后,宽度和高度被交换了。 so in addition I have override 所以除了我

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(width>height)
         gl.glViewport(0, 0, width, height);
    else gl.glViewport(0, 0, height, width);

}    

In this way i got my game back in landscape orientation. 这样,我的游戏又回到了横向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM