Android Background Service vs AlarmManager
Asked Answered
U

3

6

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

Underpainting answered 19/10, 2011 at 7:36 Comment(0)
O
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.

Odette answered 19/10, 2011 at 11:26 Comment(2)
Hmm ok got it...In which situation then we use background Service?Underpainting
Background service will be used when you want to do something in background like you want to download a huge file in you should use service so your client keep using your apps other features.Greenwood
F
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.

Fidge answered 19/10, 2011 at 7:40 Comment(3)
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?Underpainting
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.Fidge
Hmm ok got it...In which situation then we use background Service?Underpainting
C
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)

Circular answered 30/10, 2018 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.