• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Timer not honoring dynamic interval period

1taplay

Lurker
Hi All,

im working on a audio profile switcher for android and as part of the entire project, i have a service that is running in the background using the following timer code:

Code:
timer.scheduleAtFixedRate( new TimerTask() { public void run() {.....}, 0, nextUpdateInterval);
what im noticing is that the timer is not honoring the dynamically generated next update interval period...the nextUpdateInterval is declared as private static long which is initialized to 30000 (30 seconds) for the first run....then once a profile is found, i do some math and update the nextUpdateInterval...i have converted the nextUpdateInterval value back out to hours/minutes for debugging purpose, and the calculation is working as expected...like it shows me in hours and minutes, when the next timer execution should take place...


nextUpdateInterval calculation:
Code:
long entirePeriodDiff = toTimeMiliseconds - fromTimeMiliseconds;
then once a profile is found, i calculate the elapsedTime like so:
Code:
long elapsedTime =  rightNowDate.getTime() - fromDate.getTime();
and then i update the nextUpdateInterval:
Code:
nextUpdateInterval = entirePeriodDiff - elapsedTime;
one example scenario: Profile of 'Work' is set from 9AM to 4:30PM, the service/app is executed at 2:02PM (EST), my toast message is executing constantly (indicating the 'run' method in the timer is being executed) and is acting as a count down telling me how much time is left...in this case 2:28 and decreasing...ideally this should not display until the 2:28 is up...

any suggestions?

Thanks!
 
When you schedule the timer task with a particular interval, thats it. You cant just update the interval variable and expect it to react. Youd need to stop the current timertask and schedule a new one with the new interval.
 
well thats kinda lame...do i have any other options to perform this task im trying to achieve? the task is, to the timer code at a varying interval, and each time the timer runs it will perform the check to see if a new profile needs to be selected...
 
Back
Top Bottom