• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Thread vs Service

eialbur

Lurker
Please pardon the newbie nature of this question. I am a 30 year programmer, but an Android beginner.

I am writing an app that requires me to poll a server over the network (I hate polling, but it seems the best idea in this instance). There are two polling loops: one polls every 30 minutes, and one polls every one minute. Very little data is exchanged, so you see that the polling isn't terribly intensive.

From my reading, it at first seemed like I needed a Service. However, when I read about Services they were clear that I would need to use a thread in the service so I don't block the UI. But if I am using a thread, why do I need a Service? This activity is not shared - no other app would access my Service. Why don't I just use two Timers (one for each polling loop) in the main program?

Is the advantage of the service that the polling loop would continue to run even if the application is terminated? So, if the fast loop is checking for notifications then I'd want that to run in a service, but if the slow loop is only applicable while the UI is running then I could do the slow loop as a thread in the app? Given this scenario, could the Service poll start up my app under certain circumstances - or at least turn on a 'gmail'-like icon at the top of the screen?

Thanks in advance for your input.
 
It has been suggested to me that I should use Alarms instead of a long running process with Timers. Their reasoning seems sound ... however, all of the Alarm examples I've found do not appear to use a service. So, I'm kind of back to my original question.

Do I trip the alarms in a service? I'll be doing network I/O when the alarm trips, so I don't want to hang the UI. Will I still need to spawn a thread to do the actual work?
 
Back
Top Bottom