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.
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.
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.
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.
Related
I am thinking about making an android app to support a game of mine, and I can't find info on how to do it. Here is an app explanation:
The app should have two timers (both set for x minutes). When first player starts his turn (by a button click) his time starts running out. when first player makes his move, he should click his time to stop it, and player two starts his turn.
NOW, the problem I can't find a solution for - When player one takes his turn, time that he loses should pass to the other player's timer.
Example: Both players start with a 5 minute timer. If player one takes 30 second for his turn, his time should go down to 04:30, at which time he clicked on his timer. During that time, for each second he lost, the other player gained time on his timer, so at the beginning of his turn, his time is 05:30. The time goes back and forth, and the player whose timer runs out loses the game.
Any idea how to do this?
I am still stuck to the idea on how to make it, so I have no code to share.
Thank you all for your answers, and effort in helping me, if you have a question I haven't covered, I will gladly answer it.
First, you will need to instantiate class of CountDownTimer everytime you click on button.
For reference check here: https://developer.android.com/reference/android/os/CountDownTimer.html
You have two parameters long millisInFuture, long countDownInterval
Before you start, you will need to create two global variables one for first player, and one for second player like this:
long firstPlayerRemainingTime = 5 * 60 * 1000; // start time 5 minutes
long secondPlayerRemainingTime = 5 * 60 * 1000;
long limitedTime = 30 * 1000; // 30 seconds
CountDownTimer mCountDownTimer;
Now we have came to the most important part and that is logic inside onClickListener method
I don't know if there are two buttons or one, but I will go with two buttons:
btnFirstPlayer.setTag(1); // start timer
btnFirstPlayer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (btnFirstPlayer.getTag() == 1) {
startTimer();
} else {
stopTimer();
}
});
private void startTimer() {
long startTime = firstPlayerRemainingTime;
btnFirstPlayer.setTag(2); // stop timer
btnFirstPlayer.setText("Stop");
mCountDownTimer = new CountDownTimer(firstPlayerRemainingTime, 1000) {
public void onTick(long millisUntilFinished) {
firstPlayerRemainingTime = millisUntilFinished;
tvPlayerOneTimer.setText("" + firstPlayerRemainingTime / 1000)
// Here you would like to check if 30 seconds has passed
if ((startTime / 1000) - (limitedTime / 1000)
== (firstPlayerRemainingTime / 1000)) {
stopTimer();
}
// Here you would like to increase the time of the second player
secondPlayerRemainingTime = ++1000;
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
}
private void stopTimer() {
btnFirstPlayer.setTag(1);
btnFirstPlayer.setText("Start");
mCountDownTimer.cancel();
// I guess here starts second player move
}
The same logic would go for second player. Let me know if this helped you or if I need to explain anything.
In my android app I have an object which has a method which has a timer inside it. And when the method is called everything works perfectly. The count down timer in the method keeps replaying over and over again which is good but when the app is closed completely and re opened again the timer that was in the method does not keep replaying like it was before. How do i make it so the app can check if it was replaying before and if so make it replay now so it was like the timer continues to replay itself over again.
So basically: How do i make the count down timer continue to count down when the app is reloaded.
method below
public int shipAdd() {
if (counter >= addSpend) {
counter -= addSpend;
addSpend += addSpend;
counterPerSec +=addAmount;
addClick += addClick;
test++;
// The count down timer below
new TimerClass(addTime, 1000) {
public void onFinish() {
counter += addAmount;
this.start();
}
}.start();
} else if (counter < addSpend) {
}
return addSpend;
}
What you could do is use something called sharedpreferences, with this you could save where you stopped last time and pick it up from there. Also you could check if you've got a place to pick up from.
you can read more here http://developer.android.com/guide/topics/data/data-storage.html
Below I have a Runnable "updater" ...and an OnClick function that uses Handler.PostDelayed function to run the runnable after a delay...
After a little editing, cutting of useless parts here are the functions:
(passtog = Toggle Button)
final Runnable updater = new Runnable() {
#Override
public void run() {
if (passTog.isChecked()) {
now = System.currentTimeMillis();
time = now - init;
if (time > 5000) {
Toast.makeText(getApplicationContext(), "WAKE UP !",
Toast.LENGTH_SHORT).show();
}
handler.postDelayed(this, 25);
}
}
};
passTog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
init = System.currentTimeMillis();
flag = true;
handler.postDelayed(updater,
(new Random().nextInt(4000) + 3000));
}
});
}
Explaination
Basically, The user toggles the Toggle button. Now it's on: The runnable can run completely (Everything is in the if block).
If the user doesn't press the button again, and switches it off The app sends a Toast "Wake Up!" ..It runs and checks every 25 millisecs to update the time...
Pretty straightforward... Yet I'm having a problem.
Before the program actually gets to the runnable, I absolutely NEED there to be a minimum time delay of 3 seconds + Some Random value ... So it varies between 3 sec - 7 sec. It SHOULD vary between 3-7 , but it doesn't.
When I run it: The problem
I notice that the first time, it works great... I get atleast a 3 sec delay + a random value= Perfect
The second time, that is after the switch goes on ->off-> on : Now It acts like it doesn't see the +3000 ...and just the ~randInt(4000) function... So it may give 0 sec or it may give 4 sec delay...
In all my experience, I've never really come across this.. I've rewritten the entire code, My other apps use this function in exactly the same sytax and seem to do pretty great.. Why is this creating a problem ? Could the Toast's time possibly be causing a problem..
How to solve this ?
(I'm open to other methods, preferably quick to implement. I want a minimum 3 sec delay which I'm not getting for some reason... I need the UI to be responsive though So no thread sleeping.)
You probably should call Handler.removeCallbacksAndMessages(null) when the switch goes off.
I am making a little game in Processing which is similar to those Guitar Hero style games and I am trying to do 2 things:
When the game loads, stop the time from moving
During the game, allow for Pause functionality
Now, I know I cant stop the time since the millis() returns the milliseconds since the application launched, so my timer will need to be millis() - millis() at the start to equal zero, so when the user presses START, they can obviously start at the start. The game reads a file at the start, similar to a subtitles file, that has the note to be played and the time in milliseconds that it should appear on screen.
My problem is, when I pause the game, the timer keeps going and when I unpause the game, all the notes get "bunched up" due to my logic, as you'll see from my code.
Can someone suggest a better algorithm than the one I'm using? Its late and I've been working on this all day and night. I think the problem is with the for() below:
public void draw()
{
if (gameInProgress)
{
currentTimerValue = millis(); // Update the timer with the current milliseconds
// Check to see if the note times falls between the current time, or since the last loop (difficult to match exact millisecond)
for(int i=0 ; i<songNotes.length ; i++)
{
if( songNotes[i].getStartTime() > previousTimerValue && songNotes[i].getStartTime() <=currentTimerValue)
notes.add(songNotes[i]);
}
noStroke();
textFont(f,18);
drawButtons(); //Draws coloured buttons relating to Button presses on the controller
drawHighScoreBox(); // Draws high score box up top right
drawLines(); // Draws the strings
moveNotes(); // Moves the notes across from right to left
//Now set the cutoff for oldest note to display
previousTimerValue=currentTimerValue; //Used everytime on the following loop
}
else
{
drawMenu(); // Draw the Main/Pause menu
}
}
NOTE: The boolean gameInProgress is set below when the users presses the pause button, eg "P", and songNotes is an array of objects of type Note that I wrote myself. It has 2 member variables, noteToBePlayed and timeToBePlayed. The method getStartTime()returns timeToBePlayed which is a millisecond value.
Any help is appreciated.
Thanks
How about having another integer to store time when you pause and use that to offset the game timer ?
So, in 'gameInProgress' mode you update currentTimerValue and previousTimerValue and in 'paused/menu' mode you update a pausedTimerValue, which you use to offset the 'currentTimerValue'. I hope this makes sense, it sounds more complicated in words, here's what I mean:
boolean gameInProgress = true;
int currentTimerValue,previousTimerValue,pausedTimerValue;
void setup(){
}
void draw(){
if(gameInProgress){
currentTimerValue = millis()-pausedTimerValue;
println("currentTimerValue: " + currentTimerValue + " previousTimerValue: " + previousTimerValue);
previousTimerValue=currentTimerValue;
}else{
pausedTimerValue = millis()-currentTimerValue;
}
}
void mousePressed(){
gameInProgress = !gameInProgress;
println("paused: " + (gameInProgress ? "NO" : "YES"));
}
Click the sketch to toggle modes and look in the console for times. You'll notice that you only loose a few millis between toggles, which is acceptable.
Use not system timer but special timer class with pause functionality. I'm sure it is not hard to implement such class by yourself. I know that java has Timer class but unfortunately it not support pause functionality.
I want to create an incrementing second timer like a stopwatch.
So I want to be able to display the seconds and minutes incrementing in the format 00:01...
Google only brings up 24 hour clock examples, I was wondering could anyone get me started with an example or tutorial of what I want to do?
Edit:
Here is what I have using the Chronometer in Android so far
In onCreate()
secondsT = 0;
elapsedTimeBeforePause = 0;
stopWatch.start();
startTime = SystemClock.elapsedRealtime();
stopWatch.setBase(elapsedTimeBeforePause);
stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
#Override
public void onChronometerTick(Chronometer arg0) {
//countUp is a long declared earlier
secondsT = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
String asText = (secondsT / 60) + ":" + (secondsT % 60);
//textGoesHere is a TextView
((TextView)findViewById(R.id.time)).setText(asText);
}
});
In onDestroy()
#Override
public void onDestroy() {
inCall = false;
elapsedTimeBeforePause = SystemClock.elapsedRealtime() - stopWatch.getBase();
super.onDestroy();
}
The above compiles and runs but the TextView never increments, it always stays at 0, can anyone see why?
I'm assuming you aren't aware of the Android Chronometer - it already has a basic stopwatch function. You need to work with its peculiarities a bit, but it's not hard to get it to do what you want once you understand how it works.
There are a few ways that time is calculated on the phone, but the two main ones are:
The "real time", such as right now according to my computer clock, it is 11:23am in England. However, this can change if my computer contacts a time server and is told it has the wrong time, or if I were travelling with a laptop and crossed a timezone boundary. Using this would wreak havoc with your stopwatch as the measured time could change at any time.
The "elapsed time since boot", which is the number of milliseconds since the phone was switched on. This number doesn't bear any relation to the real time it is, but it will behave in a perfectly predictable manner. This is what the Android Chronometer uses.
The Chronometer is essentially a 'count up' timer, comparing the current SystemClock.elapsedRealtime() against the elapsedRealtime() that was set fot its base time. The difference between the two, divided by 1000, is the number of seconds since the timer was started. However, if you stop the timer and then start it again, you will get a counter-intuitive result - the timer will show the elapsed time as if it had never stopped. This is because you need to adjust its base time to take into consideration the time it was stopped. This is simple to do:
// When you're stopping the stopwatch, use this
// This is the number of milliseconds the timer was running for
elapsedTimeBeforePause = SystemClock.elapsedRealtime() - timer.getBase();
// When you're starting it again:
timer.setBase(SystemClock.elapsedRealtime() - elapsedTimeBeforePause);
Edit: Here is the full code for a basic stopwatch, which displays your time in a TextView rather than the Chronometer widget declared in your XML file.
public class TestProject extends Activity {
TextView textGoesHere;
long startTime;
long countUp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Chronometer stopWatch = (Chronometer) findViewById(R.id.chrono);
startTime = SystemClock.elapsedRealtime();
textGoesHere = (TextView) findViewById(R.id.textGoesHere);
stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
#Override
public void onChronometerTick(Chronometer arg0) {
countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
String asText = (countUp / 60) + ":" + (countUp % 60);
textGoesHere.setText(asText);
}
});
stopWatch.start();
}
}
In your main.xml you need to have this
<Chronometer android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/chrono"
android:visibility="gone"/>
There's undoubtedly a way to get the Chronometer to work without declaring it in the XML file, but the constructor Chronometer stopwatch = new Chronometer(this); didn't work properly.
The above code displays the elapsed time in a very basic way. For example, if only 5 seconds have gone by, it will show 0:5 rather than the 0:05 you probably want. Fixing that is not hard to do, but I'll leave that for you to work out! :)
How about this one Digital Clock.
But basically you should be able to find many java implementations by googling for keywords like java, stopwatch, digital clock, ...
You could take a look at Stopwatch Applet and modify as needed (Oops no source with this one)