There is a game where 10 minutes of real life equals 60 minutes on his day. So one day in the game equals 4 hours in our real time.
The user will inform me of the time of his game on his first time in the application, and I will need to show the game time to the user every time he enters the application. I tried doing this using a SensorService and a BroadcastReceiver, so that the calculation was always being done.
But since there is no way to get this time the same way we get the normal time, I had to do Background calculations to keep the time counting. This brought me trouble because the time was NEVER displayed accurately, not to mention the problem that is when the user has their cell phone turned off and everything.
I would really like to know if there is a simpler way to do this on Android, I'm new to this area and I do not know if there is a custom speed-time format or something. Thank you all at once.
save the first launch time(normal time).
on succeeding logins game time=(System.currentTimeinMilis() -first launch) * 6
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,
I need to do a count down timer for 24 hours but the timer needs to give equal time to all users and simultaneous.
Nobody here can really help you too much because the question is very vague and there isn't much for us to work off of, but I'll give answering it a shot and we'll see how that goes.
If I am correct, you're asking how you can make your program show every user the same timer. To do that you're going to need a server with the timer information. Each user's program should send a request for the timer information to the server whenever it needs the information and then your program does what it does with the information.
You don't even really need a timer for that. You just need to post the exact date/time of the event on a web page. In the app, on app start-up, it reads the date of the event from your web page, converts it to a Java Datetime object, then gets the current Datetime and does a date diff compare, to display how long until the event date/time. If you wish, you set a timer in the app, and every second or minute you get the current time again and update the date diff display.
I am creating an IRC bot using Pircbot that can respond to certain requests (e.g. "!time" provides local time). One of the functions I am building is a giveaway system that randomly selects a user from the currently online users and gives them a prize.
I would like to enhance the system by forcing the winner to type "!accept" within 30 minutes of winning in order to claim the prize. However I would like the bot to still function, meaning I can't freeze the entire thread for 30 minutes waiting for a message.
A few ways I am thinking of doing it feel a bit too hacky to me.
I can store the winner's name in a variable or a .properties file, and constantly be on the lookout for the "!accept" command. If an "!accept" was sent by the winner (the name in the variable) and the message sent time was within 30 minutes, confirm winner. The downside to this is if the bot restarts or is taken offline temporarily in this 30 minute period, it could cause a lot of continuity problems, especially with a .properties file.
Create a runnable thread, sleep for 30 minutes and then check all new messages for the !accept command. This sounds extra hacky with hacky sauce on top.
Dance my problems away.
Mark the time you choose the winner, maybe even have another Thread or Timer event which gets triggered after 30 minutes to reset it.
If the input is "!accept" and is from the correct user AND the difference between the "marked" time and now is less then 30 minutes, happy user
How can one ensure that a task that is supposed to run for x amount of time on an android os be run for that x period without user manipulation of date and time? For example, if I want this timer to run for 24 hours solid and then advise the user that 24h has passed, even if the phone is off for an hour, the user then turns it back on and sets the system time forward 2 hours, this timer would still indicate when that solid 24 hour period had passed without the user manipulating the system date/time and without connecting to the internet to verify the proper amount of time has passed.
Thanks
Here is flow/algorithm.
1) You need to store time of timer first initiated some where, probably database and let timer run for 24 hours.
a) assuming phone not turned off, timer continues
b) Assuming phone turned off and turned on
b1) Get initial timer started time from database, then calculate how much time left for 24 hours.
b2) Initiate timer again with left time.
I'm writing a Java Application that will side scroll sheet music across the screen and then when it crosses a middle line, it will play the note(s).
It will have a variable speed based on the beats per minute.
Every quarter of a beat I need to have a "tick" function that will get the next note (if any) and start its animation. To calculate this tick I have a function 60000ms/bpm/4. This will be separate from the animation timer, because the repaint should probably be called at some constant rate.
So I'm thinking I need Util Timer for the "ticks" and a swing timer for the JPanel and drawing in the paintComponent() method.
My question is: What would be the best practice for accomplishing this? I've done a fair amount of Java programming, but no Swing or animations so I would like to learn the best way to do this.
Thanks
Nate
There is no reason to use Timer or TimerTask to solve this problem.
In this case, you have a series of notes, and when you start playing, you can calculate ahead of time, the exact time that each and every note should play. I am assuming that you already have some sort of loop that is updating the display at a steady rate. (e.g. 30 refreshes per second, or something like that). Given such a loop, all you need is an event oriented class that can tell you whether it is time to play a note.
long noteTime[] = new long[numberOfNotes];
long startTime = System.currentTimeMillis();
Declare the above array. Then walk through all the notes in the song, and calculate the time that each note is expected to play. Say you have qurter notes, the first note will play at startTime, the second one beat after startTime, the third two beats after start time, etc. This is where you calculate the time for a beat and use it. Just calculate the actual time values for each note to play.
Then, in the middle of your event loop that is refreshing the display, include the following code:
int nextNote = 0;
while (event_loop_condition) {
.
.
.
if (System.currentTimeMillis()>noteTime[nextNote]) {
playNote(nextNote++);
}
.
.
.
}
The point is that you already have an event loop, all you need to know is whether it it time yet to play the note, and this will do that for you.
On the other hand, if you are not handling the refresh, and you really do want to do this on a thread, the follow method can be called on a thread, and it will play all the notes at the correct times:
playAllNotes() {
for (int i=0; i<numberOfNotes; i++) {
Thread.sleep(noteTime[i]-System.currentTimeMillis());
playNote(i);
}
}
The sleep statement will delay until the time that the note should be played, and will play it at that time. Then it will sleep until the time for the next note to be played.
The important thing to notice about both methods, is that any delay in playing a note is NOT compounded for the next note. Say some system utility kicks in and delays one note by 50ms. The next note will not be delayed by this, because you calculated all the times for all the notes to be played up front. Given that threads are competing for CPU time, you will have thread/process contention, and there will be small variances in the time that notes are played, but they will never compound!
A lot of programmers inappropriately use timers (See discussion of Timers) on my website if you want to know more.