So I have a web application that serves some data…lets say if I query http://www.myserver.com/?id=Apples the application returns the current price of Apples (there are many other fruits the app knows about). I am keeping the name of the fruit and the price in table on my database. The thing I do is display a list of all fruits and current prices in my main activity (which when started reads them from the DB) but also offer users the ability to place widgets on screen that display the price of 1 fruit (let’s say a small 1×1 widget).
That’s it story wise…the thing I wanna do is to keep the fruit prices updated (let’s say they every 30 min) constantly in my database. I also want my widgets to know when the database was updated to display the new price).
What would be the best pattern to implement this (this is probably very common)…AsyncTask, Service, Service + ALarm, etc. all other combinations?
You’re right, this question does come up often. The fastest way to make this work is by using an
IntentService(which automatically queues and runs your task on a seperate thread) and an alarm to trigger it.The more complete solution is to use the
AccountManager/SyncAdapter/ContentProvidercombination. The API examples included with SDK releases contains an example; additionally, this blog post appears to be quite useful.I’d like to add that since this falls into the category of a RESTful webservice, you may find the Google I/O video about developing RESTful applications to be very useful.