0

I have a question regarding AlarmManager and how to stop it when the app is accidentally closed.

My AlarmManager is repeating its alarm every 30 minute. Now when a user accidentally closes the app the alarm is still working. That is not so bad, because for a certain time it is intended to do so, but as soon as the user wants to stop the repeating alarm it is not working anymore ( a feature of the AlarmManager ). So, the user has to reboot Android.

Is there another way to stop the AlarmManager although the app is closed?

Maybe, someone has an example for me?

Thank you very much!

Best Regards, Bernd

Bernd
  • 593
  • 2
  • 8
  • 31

1 Answers1

0

You have to cancel your alarms yourself. Just call alarmManager.cancel(pi) where pi is a previously created pending intent that you used to schedule the alarm.

If you don't need the pending intent anymore, you can also cancel it by calling pi.cancel().

Another possibility is to add a check on your alarm broadcast receiver to ignore the scheduled alarms in certain situations where you don't need them anymore. This has the drawback of consuming more battery because the phone will wake up to trigger the alarms.

Ricardo
  • 7,785
  • 8
  • 40
  • 60
  • Thank you for your hint. That I have to cancel it by myself I do know, but what I want to achieve is that if a user accidentally closes the app the alarmmanager is still working. When the user now restarts the app, he or she should press a button where the running alarmmanager will be closed. However, this is not possible as soon as the app is being closed. So, I thought there might be another way to achieve my goal. Stopping the alarmmanager with pi.cancel do I know, but that is only possible if the user has not closed the app before! – Bernd Nov 18 '13 at 10:50
  • How can the user accidentally close your app? The alarm manager is a service provided by the Android system. You don't get to start or stop it. You can only schedule/reschedule or cancel your alarms. – Ricardo Nov 18 '13 at 11:00
  • The app can be accidentally closed by the user as he deletes the app from RAM ( a force closed ) - this is what I mean by accidentally closing an app. My app has one button for starting the alarmmanager and one button for stopping the alarm. But sometimes it happens that the user deletes the app from ram and so the alarmmanager is still working although the app is completely closed and when the user restarts the app he cannot trigger the button cancel because the already started alarmmanager is not reacting anymore to this. – Bernd Nov 18 '13 at 11:10
  • In this case, I think your only alternative is to have a flag saved on SharedPreferences to indicate whether your app was closed properly. If it was not, the flag will be set on your next app start and you can prevent the alarms from firing on your broadcast receiver then. – Ricardo Nov 18 '13 at 11:13
  • Check this: http://stackoverflow.com/a/17008774/362298 you cannot get any event on your app if the user force closes it. So you have to handle this some other way like I said before. – Ricardo Nov 18 '13 at 11:24