6

I see all the examples of AlarmManager being set by an Activity.

My question is this: If my application sets a recurring AlarmManager, does that persist even after the application that started is is closed and removed from memory?

If not, how do I start an AlarmManager at a lower level that is started by Android at boot up and if it ever fails or dies or throws an exception is restarted without the user having to do anything?

Lastly, if the action I wish for the BroadcastReceiver to undertake has no visual components, do I still have to create a separate Activity for it? In my case I want there to be a background uploader that wakes up and looks into a folder and if it sees files in that folder, sends them off to the server. I don't need any feedback to the user.

So, my ideal would be to have a magical, OS based AlarmManager that calls an IntentService which just handles the uploading, but I'm unclear on how to get such an AlarmManager running in the first place.

TIA

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • 1
    what happens if the app is moved to external storage and the alarm is set and then the storage is removed , how will the alarm behave in that case and how to handle this situation. – user606669 Jun 28 '13 at 14:41

2 Answers2

11

Yes, AFAIK the alarms "survive" and keeps getting triggered, even after the activity that registered them ends. But they don't survive a phone reboot.

If I understands your problem correctly, I think you can achieve what your looking for by creating a project with a broadcast receiver that listens for android.intent.action.BOOT_COMPLETED intents and then (re-)register a repeating alarm, which in turns starts a (Intent)Service to do the uploading.

You don't need an activity, but you probably would want one anyway, to let the user temporarily disable the upload mechanism by checking off a checkbox, or something. It would probably also be nice to let the user choose the frequency of your alarm, i.e. how often the service should be started and look for new files to upload. This would also be a good place to register your alarm for the first time.

  • I found all the answers to all these questions in another SO thread... http://stackoverflow.com/questions/3859489/android-running-a-background-task-using-alarmmanager. You're spot on, however. – Yevgeny Simkin Feb 22 '11 at 00:01
  • Side note: on Android 2.1 and earlier, "task killers" will wipe out your alarms. – CommonsWare Feb 22 '11 at 00:36
  • @Dr.Dredel: Regarding your question below on the competing alarms, this should not be an issue. From the javadoc for the alarm manager: "If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one." So if you use the "same" pending intent in both the application and at boot, this would ensure that only one alarm is active at a time. – Nicolai Buch-Andersen Feb 23 '11 at 20:26
  • @CommonsWare What happens if the app is moved to external storage and the alarm is set and then the storage is removed , how will the alarm behave in that case and how to handle this situation – user606669 Jun 28 '13 at 14:49
  • 1
    @user606669: Do not install apps on external storage that require alarms: http://developer.android.com/guide/topics/data/install-location.html#ShouldNot – CommonsWare Jun 28 '13 at 16:54
1

I agree with Nicolai that you'd have 2 broadcast receivers in your application :

  • one that re-register the alarm on boot
  • one that starts your service when triggered by the alarm

You could still have an activity but it shouldn't be started by the alarm receiver (hence the service) : instead, maybe launch a notification as you start your service, with the user having the possibility to launch the activity from the expanded message of the notification.

maybe also think about setInexactRepeating (instead of setRepeating) for your alarm, as well as using a worker thread to handle the long uploads (in case the user wants to use your activity in the main thread at the same time).

darma
  • 4,687
  • 1
  • 24
  • 25
  • 1
    as a side question, how do I avoid having two Alarms set (one by the application and one by boot?). I don't want them competing with each other. Is there a way to see if one is already running before firing up another one? – Yevgeny Simkin Feb 22 '11 at 01:15