Home » Android » alarmmanager – How to reset alarm if app is force closed in android

alarmmanager – How to reset alarm if app is force closed in android

Posted by: admin May 14, 2020 Leave a comment

Questions:

The alarms set by my app using AlarmManager are cleared if

  1. The user force closes my app in the settings.
  2. Some task killer app auto-killed my app process.
  3. Android itself killed my app, because of the need of memory for front apps.

Please tell me how do I detect it? rather how to set my alarms so that all these cases dont affect my alarms.

How to&Answers:

The user force closes my app in the settings.

Yes, this clears alarms.

Some task killer app auto-killed my app process.

This does not clear alarms on any recent version of Android.

Android itself killed my app, because of the need of memory for front apps.

This does not clear alarms.

Please tell me how do I detect it?

Maintain a record of when your alarm last occurred (e.g., in SharedPreferences). When your code runs (e.g., LAUNCHER activity is started), check the last-alarm time. If it was a long time ago, you know that your alarms were cleared, and so you need to reschedule them.

rather how to set my alarms so that all these cases dont affect my alarms.

This is not possible. There are few cases where the alarms actually are cleared (reboots and Force Stop), and there is nothing you can do to prevent your alarms from being cleared in those cases.