How to setup alarms again after reboot - android

I am building an alarm clock application, then is purely dependent on alerting the user during the proper time an alarm is to go off. However, reading about AlarmManager, it appears that at device reboot, all alarms are removed.
As my app would then be rendered worthless if all the users alarms were erased, how can I prevent that from happening/ensure that all alarms are always in place whether reboot or not?

Have your application handle the ACTION_BOOT_COMPLETED intent to install alarms at boot:
Broadcast Action: This is broadcast once, after the system has finished booting. It can be used to perform application-specific initialization, such as installing alarms. You must hold the RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.

Related

Why does Android's alarm manager forgets alarms?

I set alarms to notify the user on different times. I check with "adb shell dumpsys alarm" and they look like
RTC_WAKEUP #2: Alarm{433c7328 type 0 com.app.package}
type=0 whenElapsed=89936417 when=+9h8m5s417ms window=-1 repeatInterval=0 count=0
operation=PendingIntent{42e8e7f8: PendingIntentRecord{42deb070 com.app.package broadcastIntent}}
Sometimes they stay and work for at least 2 days (I didn't test longer), sometimes they are all gone after a few hours or so, even before the first notification should have taken place and "adb shell dumpsys alarm" doesn't show any of my entries anymore. (I tested with 2 devices, Android 4.0 and 4.4)
I do know that the alarm manager forgets the entries after a reboot of the device and I took care of this case and re-add them afterwards. But obviously there are other cases when the alarm manager forgets entries without having rebooted. I would like to know which cases these are and how to handle them.
From AlarmManager
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
it will work until your application has been killed or device has been rebooted.(If you removed an application from recent list, or from Application Manager you kill the app. That means you are force fully killed the application. and Then AlarmManager of your application has been removed.)
in some cases when your app is in background android kill it to access more ram.
You can read more about application kill at How to create a persistent AlarmManager, and How to save Alarm after app killing?

Android repeating notification without Alarm Manager

I have been using alarm manager for firing up notification when alarm is called. But the problem is that I have to implement particular permision to use Alarm and the user might not like this hindrance on our part.
Is there any other way to show repeating notification for ex. in crossy roads, where I don't have to use Alarm manager to repeat notification. Also I am not interested in utilizing push notification ( to avoid useless hassle).
In short:
1.) I want to notify user to start my app in regular interval
2.)Without using alarm manager and push notification.
Thanks!!!
I have to implement particular permision to use Alarm
No, you do not.
The permission to use Alarm Manager or set alarm permission.
That is only needed by the setAlarmClock() method, at most. The regular AlarmManager methods (e.g., set(), setRepeating()) do not need this permission.
Alarms get wiped out on a reboot. If you wanted to re-establish those alarms automatically on a reboot, you would need the BOOT_COMPLETED permission to arrange to get control at that time. However, you might wish to re-establish the alarms at some other time (e.g., when the user next runs your app), and so BOOT_COMPLETED is optional.

Alarm in the AlarmManager get erased when process is killed

I'm a newbie in android so please bear with me.
My Main activity creates and alarm in the alarm manager which supposed to fire in specific time, my main Activity also create Broadcast receiver which suppose to receive the Intent that the alarm fired, everything is working good until Task manager killing my App.
I've check the PendingIntent list in the AlarmManager and verify that my alarm is getting erased from the Alarm Manager, I try to add service and register alarm from the service, I've red that maybe because my IntentFilter of the Broadcast receiver is defined in code and not in manifest it get killed after app process is killed, and I'm stuck on this issue for two weeks :-(, with big confuse, my design is wrong ?
Here is my needs:
That the alarm will be very reliable, even if app is killed or even if phone is restart.
Same goes to the broadcast receiver.
Thank you in advance,
If the user task-kills or force-stops your application, your alarms are unregistered. And, on Android 3.1+, nothing of your app will run again until the user manually launches one of your activities.
There is nothing that you can do about this, other than to do your best to write a high-quality application that the user will have no need or wish to force-stop.

Reminder application

I am developing a reminder application. I am generating notifications using notification manager class, when the timeline crosses.But if my cell phone is switched off ,I am unable to see these notifications. Not even when i switch it on again.
Even if i switch it off and switch on again, i think the pending intents are destroyed and no notification is generated.
How do i get it when the phone is switched on again ?
Have a look at the AlarmManager:
From http://developer.android.com/reference/android/app/AlarmManager.html:
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

Executing code when Application is updated/reinstalled

I have Some Alarms set via AlarmManager to do some periodic jobs.
I set/reset them when user opens the app for the first time & on every boot_complete event.
But when the app is reinstalled via ADB then my alarms do not fire anymore. looks like the OS deletes the Alarms on reinstall of the app. I assume this will happen if the user updates the app from the market also.
If I can receive a broadcast or some sort of callback in the event of reinstall/upgrade etc of my application, I can set the alarms again. but i don't know if it is possible or how?
Can someone please help me out.
Yes, this is possible.
You can create a broadcast receiver that listens for any PACKAGE_* events the system sends, but you won't receive them for your own application except for when your application is being upgraded — you'll get PACKAGE_REMOVED followed soon after by PACKAGE_REPLACED.

Categories

Resources