I'm working on a CountDown app. I want to launch a 'end of countdown activity' when the countdown reaches 0. I have a variable keeping track of how much time is left (variable that I use to display the countdown).
The documentation gives me this (I have to use this method because it's part of the exercise):
public void setExact (int type,long triggerAtMillis,PendingIntent operation)
Knowing this I pass my countdown variable as the second argument thinking the activity would roughly be opened around the time I've given.
Let's say, I put a duration of 30s in the duration variable. I start the countdown by pushing a button and I use the 'setExact' method using the duration variable.
Even though, I pass 30s (obviously converted in millis), the 'end of countdown activity' launches only after 5s.
Feel free to download the project.
It sounds like you might be passing in 30000 for the trigger time, when it should probably be 30000 plus the current time in ms.
From the documentation:
triggerAtMillis long: time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type).
Related
So I have 3 games that my user is allowed to play. When they finish playing these games, just the once, I want to be able to lock them out of the game a period of time (let's say 30 seconds for now). I know you can use handler's and stuff, but I don't think it meets my requirements. When the user finishes the game, they are pushed back to the menu and a timer shows on top of the button to show how long they need to wait, the button is disabled but the others aren't (for the other games), works fine okay. If they exit out of the app or leave it, how do I make this timer continue and not reset?
If you want to transfer data across app instances, you should persist the data in disk.
In this case You can store the time of game ending in shared preference and in Activity onCreate check if the minimum time (30 sec ) has passed since the stored time,
What happens with running timers when a system time is changed?
I have an android application and I use handler.postDelayed(Runnable,interval) to post a Runnable to be called (the run() method) at the end of the interval.
The question I have is:
What happens if the underlying system time is changed externally?
My impression is that the posting still happens but at the time of system time change the countdown starts again... Can anybody shade some light here?
Does the behavior change if the time change is forwards or backwards?
First,you should know Handler is based on SystemClock.uptimeMillis().
Handlers sendMessageXXX() methods such as sendMessageDelayedăsendEmptyMessage all use the method below internal:
//calcute the milliseconds we hope to handle the message since the system was booted
sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis)
Then,the time interval value SystemClock.uptimeMillis() + delayMillis will be kept into Message's field when,and we put the message into the MessageQueue waiting for Looper to poll out it.
While the looper gets the next message from the queue,it will compare SystemClock.uptimeMillis() with msg.when to judge whether the message is ready.If the next message isnt ready,it will set a timeout to wake up until the msg is ready.
Second,you confuse SystemClock.uptimeMillis() with System.currentTimeMillis().Below is part of the documentation of SystemClock which explains the two concepts:
SystemClock.uptimeMillis() is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls), Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic, and is suitable for interval timing when the interval does not span device sleep.
System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.
No it doesn't matter. If you dig around the code, the delay is provided by the following mechanism -
SystemClock.uptimeMillis() + delayMillis
So it is purely relative. And changing of system time has no effect on it
I have a independent clock created in my application. The clock runs as a different thread in the activity, starting from a base time set by me. I update the clock using the difference between the uptimemillis when I set the clock, and the current uptimemillis. But the uptimetimer, can be reset by Android, and is ever reset when Android reboot.
I only want to know if the uptime timer is reset, to know if the clock is still reliable.
How?
According to the documentation you can use SystemClock.elapsedRealtime()
elapsedRealtime():
Returns milliseconds since boot, including time spent in sleep.
This value will only be reset when the device is restarted. Listen to the broadcast boot_complete and you will know when that is.
The problem with the updateMillis() is clearly noted in the documentation:
uptimeMillis():
Returns milliseconds since boot, not counting time spent in deep sleep. Note: This value may get reset occasionally (before it would otherwise wrap around).
From how I understand the documentation, by using elapsedRealtime your users cannot manipulate your counter.
I am making a text-based RPG for a personal project in Java. In the game the protagonist will have at his/her disposal the ability to use a spell or ability. I would like to add a cool down to the aforementioned spell or ability. I have researched both the javax.swing.Timer and java.util.Timer classes but have not found a way to go about using this cool down. I am not using threads or daemons (primarily because I don't think I need to and I have zero experience with them).
Any help or ideas on how to go about this would be appreciated, thanks.
Alumnus
Just save System.currentTimeMillis() at the time the spell is cast and check the time elapsed when the hero tries to cast it again.
When you use your spell, set some variable to say that the spell is now unavailable. Then start a timer that will act as your cool down. When the timer ticks, reset that variable to indicate that it has cooled down and can be used again.
If possible you could save the current game time when the spell/ability gets triggered inside your application and disable the ability.
Then again if yo can do it you can check every "frame update" if the cool down time has already elapsed by computing the time difference between the current time and the time the spell/ability got triggered and reenable the ability when the desired time has elapsed.
If this RPG is turn based combat then do this:
Make a cooldown variable with the number of turns needed to wait for cooldown.
Now make another variable for current cooldown turn, now set its value to the same as the cooldown like
curcooldown = waitforcooldown, something like that.
And then if you check that the player wants to use the spell and then check it like this.
If player wants to use spell & curcooldown is 0
UseSpell
Else if (player wants to use spell & curcooldown greater or equal to 0
saytoplayerthatspellisundercooldown
and if spell is used then curcooldown = waitforcooldown
set it to the cooldown value turn value.
and every time a turn ends then decrease the curcooldownvalue by 1 that means your cooldown just lost 1 turn and so on...
Hope this helped
As the Count Down timer pauses, how would you store the value?
which then when you want to resume the timer eventually will restart a new timer
with this stored value. I am so confused how to hold the value.
I am a beginner in java and android programming.
could any one guide me.
Android has inbuilt class for count down i.e. CountDownTimer class . You can use this.
http://developer.android.com/reference/android/os/CountDownTimer.html
Countdown timer doesn't have a pause() method. Workaround that i can think off is try getting the time at onTick().
So you have an application displaying count down timr, and like that it continues countdown from very same moment after being paused? In this case you have to save your
remaining time in some location in onPause() method ( best location would be shared preference )
Or do you like to have real countdown time and fire some event after it expires, even if your application is inactive? Then schedule an alarm at desired time, and store scheduled time somewhere (shared preferences would be ideal). In this case you can forget keeping time yourself, just update UI periodically using real time and stored time difference