简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM