Shane
Shane

Reputation: 2375

Service or BroadcastReceiver?

First time I have tried to implement either of these and I am unsure which to use.

I want my application to create a time frame, e.g. 2/3/12 to 7/3/12. Multiple time frames such as this can be created. A different intervals (e.g. every 4 hours) I would like my application to preform some actions for each time frame. This needs to be done in the background.

I have first tried to implement this with a Service, but am having performing all the actions for each of the time frames concurrently. After reading the android blog "Multitasking the android way" I think that perhaps BroadcastReceivers are better.

Please advise

Upvotes: 1

Views: 345

Answers (2)

rogermushroom
rogermushroom

Reputation: 5586

You should probably be using the AlarmManager and a IntentService.

The AlarmManager will kick of your IntentService at specified intervals. You can kick off the IntentService for each set of actions.

Upvotes: 2

Graham Smith
Graham Smith

Reputation: 25757

Please see my answer about using the AlarmManager - Running task periodicaly(once a day/once a week)

If you are only running a process at a set time rather than constantly (e.g. monitoring audio levels) then you are going to ask a service to sit there 90% of the time and do nothing except waste battery power. The AlarmManager solves this problem as it notifies the broadcast receiver to execute at the given times.

Edit: Also bear in mind that after phone restart all alarms are removed so you will need to register a broadcast receiver to be notified of the device boot-up so you can re-register any Alarms that are needed.

Upvotes: 2

Related Questions