How to run a code periodically the right way
I need to download some data and parse the results at some intervals. Right now i start a service from my actitivy that starts a TimerTask every few seconds. Now i read so开发者_开发技巧me things about AlarmManager and handler and got confused with so many ways of solving this kind of problems.
Is this the right way to do this kind of things?
For what kind of tasks is AlarmManager and for what kind of tasks is Service (with TimerTask)?
AlarmManagers are used when you want your app to do something at specific intervals even when your app is not used. AlarmManagers broadcast the event and starts the intent thats attached to it which will be your app.
Timers and TimerTasks are used when you need to do some operation during intervals while your app is currently used. So when your app is closed you cancel all the timers. And it will only start again the next time you open the app.
精彩评论