简体   繁体   中英

CountDownTimer running even when user switches app

I'm building an Android game and I need to display a countdown. In order for the users to not cheat I can't stop this countdown if user changes app, because they would have more time to think the answer.

What I need to do is to let the CountDownTimer continue working even if my app is not currently on front (onPause has been called).

I wonder if the code I have (which seems to work) will do the trick, or if under some circumstances it would fail.

Right now the countdown continues and when it gets 0, the result screen is displayed over the current app (which, of couse, is not what I want). I think for this second question the solution would be to store a variable or something and check that before launching results screen.

So, to sum up, what I want is the countdown to continue even if the user changes the current app, but when the countdown gets to 0, don't show the result screen until user comes back to the game.

My code is as follows:

new CountDownTimer(10000, 1000) {

   public void onTick(long millisUntilFinished) {
       tvTime.setText(String.valueOf(millisUntilFinished / 1000));
   }

   public void onFinish() {
       startActivity(new Intent(GameActivity.this, ResultActivity.class));
   }
}.start();

Thank you.

Storing it in a variable and displaying it as long as it's not paused seems like the simplest solution. What I'd do is in the onPause method set some global variable to true, and something like

if(!thatGlobalVariable)
{
     displayVariable()
}

You know what I mean, it's your idea relayed back at you.

Sorry this wasn't much help, and you were probably looking for some other more extravagant solution. I'd do this in the meantime until you get an answer that is more suitable.

Also, I think it's pretty cool if the timeout message pops up when minimized. So the person might be looking up something in google then all of a sudden "Time up, you failed". People like games that torment them into playing more.

Just to give you an idea of what you should do. Probably should use SystemClock.elapsedTime() .

public class CountdownTimer {
    private final long startTime = currentTime();
    private final long totalTime;
    private boolean isFinished = false;

    public CountdownTimer(long millis) {
        totalTime = millis;
    }

    public long timeLeft() {
        return totalTime - (currentTime() - startTime);
    }

    public boolean isFinished() {
        return isFinished || (isFinished = timeLeft() < 0);
    }

    protected long currentTime() {
        return System.currentTimeMillis();
    }
}

If the user closes your app then you shouldn't reopen it on them. Let them open the app themselves and find out that their time is up, or display a notification.

Along with all the other game state you save in the SavedInstance you could save the current time. When user resumes your app you can compare the new time against the saved value to determine whether the time is up. Then the message can be displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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