1

I have an AlarmManager that triggers an alarm every X minutes.

When the alarm is trigger I'm doing a few operations like reading a small file and updating the SharedPreferences.

What is recommended to use for these operations in this case?

A service seems fitting but since these are relatively short operations, maybe a BroadcastReceiver is better (lighter?)?

It seems that both BroadcastReceiver and a Service are running on the same process and on the UI thread and the only difference is how and when Android is killing them. Is that correct?

As far as I understand the AlarmManger can either call a Receiver or a Service (or an Activity but that's irrelevant now). My alarm runs always, even when my app is not running. So either way (a service or a broadcastreceiver) will start my process if it's my app isn't running. The alarm can go off every even 10 seconds, it depends on the user. So in that case, IntentService will have to start a thread every 10 seconds. Isn't that "harsh" on the system? Thanks.

Ran
  • 4,117
  • 4
  • 44
  • 70
  • In my app I have use BroadcastReceiver with AlarmManagar. don't no which is better service or BroadcastReceiver. @Ran – Pankaj Kharche Feb 22 '14 at 08:46
  • For the record, it's not going to be hard to "switch" between either if you want to try. Creating and using a service is relatively trivial :) – Martin Marconcini Feb 22 '14 at 22:12
  • @MartínMarconcini , I agree but I'm not sure what I should measure to decide which one is better :) obviously both will work fine. – Ran Feb 22 '14 at 22:17
  • Testing, a lot of testing :) In general, go for the simple approach (BR). If you feel that your task(s) are not being executed or something is not ok, then consider a Service… that's my 0.2¢ :) – Martin Marconcini Feb 23 '14 at 08:55

2 Answers2

2

If you are already using an AlarmManager, then use a BroadcastReceiver in whatever Activity (or Fragment) has the code to perform the operations you mention. Remember that either will run on the UI Thread, so use an AsyncTask or Thread.

You don't need a service running because your alarm will be triggered by the AlarmManager. No need to spawn a new process (Service) for that.

You could approach the problem from another point of view (if you use a Service) by not using AlarmManager and post a Runnable every X minutes, but the first approach should be ok.

UPDATE:

Perhaps you misunderstood me. A service can run in a different process (although it doesn't do it by default), but it's different from a BroadcastReceiver. If you are using an AlarmManager, there's no need to have a Service running.

The difference between Service and BroadcastReceiver is usually a source of confusion among new Android devs.

Without knowing the nature of the work you need to perform every X minutes, it's hard to tell what the best scenario is.

As long as you understand the consequences of running a Service (in its different modes) and/or how not to block the UI thread, then yes, using a Service is usually good, even an IntentService can help you.

Perhaps you want to couple your AlarmManager with an IntentService, but beware that this doesn't prevent the device from going to sleep, you might as well use a WakefulIntentService that will keep a wakelock for you.

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • A service runs on the same process and thread as the UI, so it's not a new process. It seems that all the difference is when Android is killing it. – Ran Feb 22 '14 at 08:54
  • I updated my question to be more clear, thanks. – Ran Feb 22 '14 at 10:43
  • If the nature of your task is "do a couple of things every now and then" I don't think you need a Service. Services are meant to perform an action in the background for a period of time, regardless of what the user is doing in foreground (even if he's not in the app at the time). An intent will be broadcasted to all receivers that have matching intent filters. But Broadcast receivers do something and are done. (until you call them again). If there can be "minutes" between each alarm, I'd go with a BR. – Martin Marconcini Feb 22 '14 at 21:58
  • Allow me to offer you a more complete response: http://stackoverflow.com/a/14549042/2684 – Martin Marconcini Feb 22 '14 at 22:02
1

A broadcast receiver (short receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens! yes u can use it

Amin Bahiraee
  • 538
  • 10
  • 20