Android Service that uses web service data
I am working on android application in which I have created one service that fetch data from web service but the service fetch data when I click on the button to start service.what I want is my service fetch data from web service 24 hours because data on web service updated开发者_开发百科 at any time.so should I use timer or anything else please give me solution,thanks
You can have may be a thread inside the service that runs only when a boolean variable is true. You can change the value of this boolean variable to true when ever you click on the "button" that you mentioned.
There is also one other scenario. When the user clicks BACK from the main activity, you can set this boolean to false, so that will actually not keep fetching data from the web service un-necessarily.
And onResume you can set that variable back to true. So this solution wouldn't waste precious battery life and will also ensure that the user has the data always.
You can use the AlarmManager
class which will call your web services after a particular time which you set.
精彩评论