0

I have an alarm app which I think was programmed in the standard way using an AlarmManager and BroadcastReceiver. I have even added code so that the alarm stays active after a reboot. I have been using the program without problem for weeks now.

I had been led to believe that doing things this way meant that it was not essential to keep my alarm program running the whole time, but to my horror I have just found that if I set an alarm and then do a force stop of my alarm app, then the alarm never rings.

I'm not sure that this means I made a mistake in my coding, or I was misled into believing that it is possible to keep the alarm set despite the app being stopped.

Maybe the OS somehow detects that my alarm app has an alarm running and therefore avoids shutting it down? So as long as the user doesn't manually do a force stop then all should be well.

Any ideas?

EDIT: I just tested "Alarm Clock Xtreme". I set an alarm. Then did a force stop... the alarm never rang.

Mick
  • 8,284
  • 22
  • 81
  • 173

1 Answers1

3

This is the expected behavior if your application is force closed. However, the alarm should continue working as expected if the user leaves the application through normal means like the back or home buttons.

See: Android alarm is cancelled after closing the application

Community
  • 1
  • 1
Eric Levine
  • 13,536
  • 5
  • 49
  • 49
  • OK, but on devices with low memory the OS will regularly stop not-currently-visible activities. How do I know it will not stop my alarm app?.. Do I need to set a flag saying "please don't shut me down unless desperate!" – Mick Nov 02 '12 at 16:35
  • In a nutshell, you'll want to create a Service which handles the playing of your alarm in response to the Intent triggered by AlarmManager. Services are meant to handle longer running background operations and are much less likely to be destroyed due to low memory. If the OS has to destroy your Service, it will try to re-create it as soon as it can. There are flags which you can pass to tell Android how to re-create your Service. Take a look at IntentService, which is very easy to use: http://developer.android.com/reference/android/app/IntentService.html – Eric Levine Nov 02 '12 at 20:13