Preventing timer manipulation from end user with android TimerTask and Timer - java

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.

Related

How do I make a timer that gives the same time for every user even if they close the app

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.

Working with Different Time Speeds

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

Performing a task everyday using TimerTask doesn't work for the next day.(with other problems)

I have a TimerTask that updates a few tables everyday at specific times(not one task per day, different tasks at different times in a day). I get a few time periods from a database and check if the current time is equal to one of the times in the database when the run() of TimerTask is called and perform the respective task for respective time(time from database which is equal to current time)
I think it's not working for the next day because I set the time(which I obtain from database) for the timer when the service starts. The next day when the timer is supposed to do the task and checks with the current time it checks with the previous day's time(the day service is started and time is set for TimerTask) and does not do the task. I should start the Service everyday to set the time(which is obtained from database to check with current time).
How do I start the service everyday at specific time so that the time to be checked with current time is set everyday.
I can try to schedule to do the tasks everyday 24 hours from the day service is started, but sometimes I have to change the start date and for that the service should be started again and I dont know if a service will run for 24 hours.
You should use Alarmanager for this. The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

Java timer task: Adjusting daylight saving time

I have a piece of code that has to be executed at a particular time every day. If I schedule it to be executed at 9PM everyday, then it has to work even during the switching of Day light saving.
Which Java API can be used to achieve this?
int ONE_DAY = 1000 * 60 * 60 * 24;
Timer timer = new Timer();
timer.schedule(myTimerTask, startTime, ONE_DAY); // startTime is 9PM of current day
I've used the above approach which will not take care of DST.
If you need to schedule based on calendrical values - rather than just elapsed time, basically - then you either need to wrap Timer in your own code, or use a library which has already been built for this purpose. In this case, I suspect the Quartz Scheduler is your best bet.
Given how complicated date/time can be, I'd generally recommend using a well-known library over rolling your own code. Note that this often doesn't mean that you can get away without thinking about complicated aspects of the problem - it just means that you should be able to express your requirements fairly simply. For example, in the context you're looking at, you should consider:
What time zone do you want the "9pm" to be expressed in? Is it the system default time zone? Some other specific one? Multiple different time zones for different tasks?
What do you want to happen if the scheduled time doesn't occur, or occurs twice on one day? You're likely to be okay with 9pm, but if you had (say) 1.30am in the UK time zone, when the clocks go forward into BST, that will be skipped for that day - and when the clocks go back into GMT, it will occur twice.
How do you want to handle the system clock being changed, either manually or automatically?
You can schedule the timer to run the task each hour and let the task decide when to actually run using Calendar

java.util.Timer SystemTime effect?

Said now is 6 o'clock, I have a Timer and scheduled a TimerTask at 10 o'clock. After that, System DateTime is adjusted by an other service (ntp for example) to 9 o'clock. I still want my TimerTask will be fired at 10 o'clock but it does not, Timer still wait for next 4 hours and fire my TimerTask. What should I do in this situation?
Firstly, you're already in a pretty nasty mess if your clock is out by 4 hours. Typically time adjustments will only be by milliseconds or seconds - or occasionally a minute or two, if the machine hasn't been online for a very long time. One option would be to check that the time is reasonably accurate by making your own NTP call before setting the timer.
Another option is to make a reasonably regularly-invoked timer - for example once every minute or five minutes - which checks the time and then optionally takes action. It's slightly less efficient, but I wouldn't expect the impact of waking up a single thread to perform a simple check once a minute or so would have a significant effect on performance. You should adjust the regularity of the check based on how accurately you need your timer to fire, and how little performance impact you need it to have.

Categories

Resources