power key点亮屏幕时,Activity会收到短暂的onResume(之后LockScreen出现)
Google建议onWindowFocusChanged()与onResume()配合使用,具体做法如下
Here’s the easy two-step way to avoid user embarrassment:
-
Pause the game (and all sound effects) whenever you receive an onPause() message. When gameplay is interrupted — whether because the phone is locked, or the user received a call, or for some other reason — the game should be paused.
-
After the game is paused, require user input to continue. The biggest mistake most game developers make is to automatically restart gameplay and audio as soon as the user returns to the game. This isn’t just a question of solving the “music over lock screen” issue. Users like to come back to a paused game. It’s no fun to switch back to a game, only to realize you’re about to die because gameplay has resumed before you expected it.
Some game designers don’t like the idea of pausing the background music when the game is paused. If you absolutely must resume music as soon as your game regains focus, then you should do the following:
-
Pause playback when you receive onPause().
-
When you receive onResume():
-
If you have previously received an onFocusChanged(false) message, wait for an onFocusChanged(true) message to arrive before resuming playback.
-
If you have not previously received an onFocusChanged(false) message, then resume audio immediately.
-
-
Test thoroughly!