Take the 2-minute tour ×

I have developed a game using andengine

I am facing a problem when using the power Button. If I press the POWER Button while playing the game the screen turns off and the onPause() is called as expected. But when I press the HOME button or POWER button again to turn on the screen, onResume() method is called and but the lockscreen shows up.

In the onResume() method, I resume the music of the game.

So as a result lockscreen shows up , but the game music plays in the background. I do not want to play the music in the lockscreen. Please help me solving this.Thank you

share|improve this question
android-developers.blogspot.de/2011/11/… –  user658042 Aug 30 '12 at 13:36
@alextsc perfect guide –  rajpara Aug 30 '12 at 13:47
@alextsc its a good tutorial..thank you. But for my case it doesn't do the job –  Ajitha Aug 30 '12 at 19:31
add comment

1 Answer

up vote 2 down vote accepted

The solution is here: Activity handle when screen unlocked

By registering a BroadcastReceiver filtering the Intent.ACTION_SCREEN_ON, Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_PRESENT actions, you will be able to handle these 3 cases:

  • Intent.ACTION_SCREEN_OFF: When the POWER button is pressed and the screen goes black.
  • Intent.ACTION_SCREEN_ON: When the POWER button is pressed again and lockscreen is showed up.
  • Intent.ACTION_SCREEN_PRESENT: When you pass the locksreen and are back in your game.

However, in my case (using a Galaxy S GT I9000 with Froyo 2.2) these actions are not called when dealing with the HOME button (and I think it's a general behavior).

One simple and quick (but maybe not the best) way to handle both HOME and POWER buttons to pause and resume your music could be to keep the onPause and onResume methods and use a simple boolean flag like this:

private boolean mPowerButton = false;

public void onPause() {

    // Pause your music
    Log.d("Game activity", "Music paused");

public void onResume() {

    if (!this.mPowerButton) {
        // Resume your music
        Log.d("Game activity", "[HOME button] Music resumed inside onResume");

public class receiverScreen extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // This is the lockscreen, onResume has been already called at this  
            // step but the mPowerButton boolean prevented the resumption of music

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            LevelActivity.this.mPowerButton = true;

        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
            // Resume your music
            Log.d("Game activity", "[POWER button] Music resumed inside ACTION_USER_PRESENT action");
            LevelActivity.this.mPowerButton = false;

Hope it helps!

share|improve this answer
Thanx for your reply but I could not make this work for me. Anyway I have fixed this out by using OnWindowFocusChanged() method instead of using OnResume(). Thank you –  Ajitha Sep 3 '12 at 6:11
add comment

Your Answer


