Best way to run a check periodicly?
I am about to write a task which will be run once in about 1 - 10 minutes. This task uses the GPS. However I am not sure about the best way to do this. Is it better to use a Service in the background with a sleep or timeout of some 开发者_StackOverflow中文版sort? OR am I better off using the AlarmManager Class from the android SDK?
Keep in mind it is important the task is always run (cannot afford a task to be missed because of android freeing up resources).
AlarmManager -- see the section "Class Overview". An Intent will be sent to your program and will automatically start it if it got destroyed.
There are no hard guarantees for a Service -- it can get destroyed under extreme memory pressure. Usually not an issue, so I guess it depends on how paranoid you are about that. Also, if a Service does get destroyed, I think Android tries to restart it whenever possible.
The AlarmManager doc also suggests Handler class. Give that a look also.
精彩评论