2

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

Akshay
  • 2,506
  • 4
  • 34
  • 55
Ajitha
  • 239
  • 3
  • 10
  • 2
    http://android-developers.blogspot.de/2011/11/making-android-games-that-play-nice.html –  Aug 30 '12 at 13:36
  • @alextsc its a good tutorial..thank you. But for my case it doesn't do the job – Ajitha Aug 30 '12 at 19:31

1 Answers1

2

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;

@Override
public void onPause() {
    super.onPause();

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

@Override
public void onResume() {
    super.onResume();

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

public class receiverScreen extends BroadcastReceiver {

    @Override
    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!

Community
  • 1
  • 1
Michel-F. Portzert
  • 1,785
  • 13
  • 16
  • 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 03 '12 at 06:11