Search code examples
javaandroidlibgdxonpause

How to prevent onPause() when pressing home button?


I am working on a libGDX app that include a timer. I need the timer to be running even if the home button is pressed, and even if the screen is turned off by pressing the onoff button.

For that I thought about preventing the app to go in the paused state. In the AndroidLauncher.java I add this lines of code :

@Override
  public void onPause() {
        onResume();
  }

The idea is to resume the app as soon as it goes in the paused state.

Here is the logcat of the app after pressing the home button and going back to the app :

01-26 18:17:25.125: I/AndroidInput(2753): sensor listener setup

01-26 18:17:25.126: I/AndroidGraphics(2753): resumed

01-26 18:17:25.191: W/IInputConnectionWrapper(2753): showStatusIcon on inactive InputConnection

01-26 18:17:28.899: I/AndroidInput(2753): sensor

01-26 18:17:28.919: I/AndroidGraphics(2753): resumed

As you can see, when the app is running, if I press the home button, the app resumes, without going on paused state, and when I go back to the app it resumes again. But, the timer is stopped...

Why the timer stopped while the app never whent in the paused state ? And finally, how can I keep the timer running after pressing the home button ?

Thanks


Edit : So I started to use the services and I still encounter problems to have the timer running in background.

Here is the startegy :

  • In my GameScreen, I have a timer, where I can enter a number of seconds,
  • then I use a task to countdown to 0.
  • As I am working on a libGDX project, I use an interface to communicate the current number of seconds to the AndroidLauncher, when the game goes in paused state.
  • That interface also triggers a service that keep the countdown running with the same timer and task, and it prints the current number of seconds in the consol every seconds.

Code :

Timer in the GameScreen.java :

timer = new Timer();
timer.scheduleTask(new Task(){
        public void run() {
            timerSeconds--;
        }
    }, 1,1);

Interface ActionResolver.java :

public interface ActionResolver {
    public void backgroundTimer(int a);
}

The ActionResolver is called in the main activity MyGdxGame.java :

@Override
public void pause() {
    super.pause();
    actionResolver.backgroundTimer(GameScreen.timerSeconds);
}

The backgroundTimer method is deffined in the AndroidLauncher :

@Override
public void backgroundTimer(int a) {
    aa = a;
    
    Intent intentTimer = new Intent(AndroidLauncher.this, IntentServiceTimer.class);
    intentTimer.putExtra(TIMER, aa);
    startService(intentTimer);
}

Then I created the intentServiceTimer class :

public class IntentServiceTimer extends IntentService{
private final static String TAG = "IntentServiceExample";
int a;

public IntentServiceTimer(){
    super(TAG);
}

@Override
protected void onHandleIntent(final Intent intent) {
    Log.d(TAG, "When the game went on pause, the number of remaining seconds was : " + intent.getIntExtra(AndroidLauncher.TIMER, -1));
    
    a = intent.getIntExtra(AndroidLauncher.TIMER, -1);
    Timer timer = new Timer();
    timer.scheduleTask(new Task(){
        public void run() {
           a--;
           Log.d(TAG, "Seconds remaining : " + a);
        }
    }, 1,1);        
}
}

In the AndroidManifest.xml I declared the service :

<service android:name="IntentServiceTimer"/>

Results :

When I lunch the app, this line of the Service prints the right message as soon as the game goes in paused state, which is good :

Log.d(TAG, "When the game went on pause, the number of remaining seconds was : " + intent.getIntExtra(AndroidLauncher.TIMER, -1));

But, the timer loop, which includes the countdown and a print of this countdown every seconds, seems to doesn't work, as nothing prints in the console. But, if I go back to the app, the countown prints, in one time, every seconds that the game was in paused state. For example if I put the game in paused state for 5 seconds, as soon as I go back to the app it will print :

Seconds remaining : 27

Seconds remaining : 26

Seconds remaining : 25

Seconds remaining : 24

Seconds remaining : 23

So I have this weird behavior of the service :

  • print the 1st message when expected (when the game goes in paused state)
  • The countdown seems to run in background
  • The result of the countdown prints only when resuming

Do you have any idea about that problem ? I still didn't check the alarmmanager, as I'll need to have more elaborate process running in the background, in the future, the services seem to be what I need.


Solution

  • You should use a service to solve this: Services

    Other method that is called when you press some of three soft button is onUserLeaveHint, but it can't solve your problem, by the way you can use it in future sure:

        protected void onUserLeaveHint() {
            super.onUserLeaveHint();
        }