Hades
Hades

Reputation: 3936

service implementation pattern

ok I have this application which needs to send periodic updates to a web-service, I have done a fair amount of research and I've come up with two service implementation patterns.

  1. Implement a service with a thread, the periodic update time may vary therefore, I will put the thread to sleep with the required time-interval, then call the web-service again. I also need to update an activity, therefore will be using a broadcast receiver or a messenger.

  2. Use a Service with a schedule timer/alarm manager, wake the system and use intent services coupled with a broadcast receiver.

Which would be the best approach?

Upvotes: 0

Views: 130

Answers (2)

500865
500865

Reputation: 7120

I think I would go with the #2 option :

  1. Create an IntentService to do the update.
  2. Register a BroadcastReceiver with IntentFilter(s) and start the IntentService from it.
  3. Use AlarmManager to Broadcast the registered action at the required time intervals.

I prefer this method because :

  1. It is a very flexible pattern : You can start the service anytime by registering the same receiver for different actions like network connection changes, system boot changes etc.,
  2. It keeps the logic loosely coupled from other parts of the app.
  3. There is no hassle of managing threads. You get it for free by using IntentService
  4. It is more android-ish way of solving this problem.

Upvotes: 2

Rich
Rich

Reputation: 36836

The main difference would be that a background service can be shut down by the user and then you won't get any more updates. If you register events with the AlarmManager, then you control when/if these events take place. If the user shuts down your app and goes into a task manager and shuts down any running services related to your app, the AlarmManager is still going to wake up and send a message that your BroadcastReceiver will receive.

Upvotes: 0

Related Questions