5

Can someone give a little bit briefing or perhaps more elaborate details on differences Android background service with Alarm Manager?

How they differ? And in which situation I should use each?

I am developing an application that need to download data from Web Service at periodic time. The application has few modules and each modules has different interval time period to download / sync the data to Web Service.

Let say + Module A need to sync in every 15 mins + Module B need to sync in every 1 hour + Module C need to sync in every day + Module D need to sync weekly + Module E need to sync monthly

Which approach is better? And why?

Regards

stuckedunderflow
  • 3,551
  • 8
  • 46
  • 63

3 Answers3

8

AlarmManager schedule the intents. Your service can crash and be removed if memory is not available. But with AlarmManager you can invoke your specific service at a scheduled time. In your context, AlarmManager is a better option than a running service.

Hope it will help you.

Allan Veloso
  • 5,823
  • 1
  • 38
  • 36
Cool Java guy מוחמד
  • 1,687
  • 4
  • 24
  • 40
0

There's a very good analysis on this new blog post: https://android-developers.googleblog.com/2018/10/modern-background-execution-in-android.html?linkId=58286424

For sync on regular basis you might end up using WorkManager, started on specific time or when triggered by a push message (FCM)

Gusthema
  • 5,436
  • 1
  • 15
  • 15
0

With the help of the AlarmManager you can schedule intents. Your service or a BroadcastReceiver can listen to these scheduled intents and perform tasks at the given time. You can not only use the AlarmManager to implement program logic.

You can use the AlarmManager to dispatch an Intent every 15 minutes with an action for A, one every hour for B and so on. Either your modules are implement as independent services or you decide on the intent action what to do.

Please keep also in mind that long operations should not be performed in a BroadcastReceiver.

senola
  • 782
  • 4
  • 12
  • So the activity that extends Service is same like BroadcastReceiver? I read somewhere that Android Service is not recommended, but use Alarm Manager. What does it means? – stuckedunderflow Oct 19 '11 at 09:22
  • No its not the same as a BroadcastReceiver. For your problem you can probably forget all the stuff about BroadcastReceiver. The recommendation to use the Alarm Manager means that you should not have a Service running all the time waiting for the specific time interval but to use the AlarmManager to dispatch an intent after the specific time which will then start your service for your task. By that the resources of the device wont be used that much. – senola Oct 19 '11 at 09:33
  • Hmm ok got it...In which situation then we use background Service? – stuckedunderflow Oct 20 '11 at 01:52