i’m currently working on an app for the android os that requires to fetch data from a remote server from time to time.
as this “update” should be carried out even when the actual frontend app is not running, i implemented a remote service that is started on system boot. now i need to schedule a timer to start the update.
is the “Timer”-class the right one for this job? and if “yes”: what is the difference between a “normal” Timer() and one started as a “daemon” by Timer(true)?
http://developer.android.com/reference/java/util/Timer.html isn’t very helpful with this 🙁
EDIT:
ok – i see there are much more methods to do this than i expected. to clarify:
- i want to execute some code at a time that is specified.
- this timer is used to trigger the execution of code 7 days in the future. (i.e., every week at a given weekday and time)
- the code should run WITHOUT waking the phone up if it is “sleeping” (screen dimmed).
- when running the code, no activity should be started. i.e. no app pops up on the screen.
- the code that is executed should fetch some data from the internet. if at this time no internet connection is available, the timer should be set to sth like 30 minutes and then try again.
- after completing the code execution, the timer will be set for the next interval which will be 7 days later.
- the timer should be started at system boot, e.g., if i reboot the phone, the timer should determine the next date to execute the code and schedule the timer. this has to work without ANY user interaction!
-
when “sleeping”, the thread/service/timer/whatsoever should not consume any system resources if possible…
-
what i need is pretty much a simple unix cronjob.
i think anyone here knows “newsrob” for android? what i want to realize is pretty much the same as the newsrob-updateservice.
Use
AlarmManager. This allows you to set your schedule, then exit your components. Your code does not need to remain in memory and will be triggered when the alarm sounds.Please don’t do that just for a scheduled task. Use
AlarmManager.If you want the work to be done while the phone is asleep, you will need to use a
_WAKEUPalarm type and perhaps use something like myWakefulIntentServiceto keep the device awake while the work is being done.