11

Documentation for AlarmManager startes that

Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

However, once my application is closed (force close from task manager) my alarm does not work and the OnReceive method is never called inside the broadcast receiver. I am targeting 4.x.

What's happening?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
tony9099
  • 4,567
  • 9
  • 44
  • 73
  • please show us how you are setting up the alarm, and the entry in your manifest for the receiver of the alarm. – Pork 'n' Bunny May 06 '13 at 15:01
  • 4
    When a force close occurs, all the objects associated with that app are cleared and hence the alarm manager also gets cleared. The documentation has info when app is in "normal" condition, i.e. its not force closed. The alarm manager can work even if app is not running. – Shrikant Ballal May 06 '13 at 15:01
  • @Shrikant What is the difference between "app not running" and "app force closed"? – berserk Jan 21 '15 at 23:34

1 Answers1

14

What @Shrikant says is pretty much the answer.

The longer verison is that it is assumed by Android that something is amiss with the app if the user had to force close it manually. Therefore all activities (BroadcastReceiver's, alarms, etc) related to the app will not be initiated until the app is run manually by the user at least once. For example, app's boot BroadcastReceiver will not be called when device is turned off and on in this state until the user runs the app, then the next device boot event will be delivered to the app's BroadcastReceiver.

This behavior is confirmed as by design by Android framework devs: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/anUoem0qrxU

*edited for grammar & added an example behavior

Kai
  • 15,284
  • 6
  • 51
  • 82
  • 1
    Ok this is odd to me. Maybe the user killed an alarm clock because they wanted to free up resources, but they still want the alarm clock to go off next morning? How does this work? – b.lyte Feb 04 '14 at 23:07
  • @clu no the user does not kill alarm clock, if an user killed a misbehaving app or that app crashed by its own misbehavior, Android would stop everything related to that app to protect the user from been (further) harmed by the app. Removing the app from system alarmclock is part of that procedure. – Kai Feb 05 '14 at 01:23
  • 2
    @kai : Do the alarm managers get killed when the app crashes? If so,should we set the alarms in the application class/ or what is the best approach to take in such situations? – Basher51 Jul 24 '14 at 04:39
  • @Basher51 great question. Should we reschedual all the alarms on application class? – motis10 Oct 31 '17 at 13:24