I want to create an Alarm for Android.
I've used so far an AlarmManager that sets on the given time and day.
And onReceive(), I have a NotificationManager that warns for a specific event.
My problem is that if the event is set for a week or month later, I may restart my device and my alarm will be lost.
Can someone explain me how can I save this alarm and restore it after reboot?
you need to use a broadcast receiver that listens for the broadcast
android.intent.action.BOOT_COMPLETED
then restart your alarms from your broadcast receiver
Related
My app runs in background. I use alarmmanager to trig a alarm every minutes.When my Broadcast receiver receive the alarm I start a service.But I found that the service only last for about 4~5 seconds.I think every time the alarmmanager trig a alarm and a new Broadcast receiver instance created.the Broadcast receiver goes quickly.So the service gone with it?But the android.os.Proccess.myPid() is always the same.I am confusing with it.
Thanks,
zhangkai
I have a broadcast receiver for receiving the 'BOOT_COMPLETED' event. What I want to do in the OnReceive method is to re-schedule an alarm whose time is pre-defined by the user. How would I go about getting the alarm time that the user entered into the application? I tried looking at SharedPreferences but these don't seem to be accessible out-with Activity classes. Does anyone have any ideas on how I could go about getting this information?
You don't have to set the alarm at boot time. You can use AlarmManager to handle that for you. You can even set repeating alarms with it.
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
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.
I'm trying to make a task schedule app and I made an Alarm app trying to learn how to do that part at least. It uses AlarmManager and it makes an alarm go off at a time chosen by a TimePicker. But it doesn't work when the emulator is turned off and on again.
So I'm trying to use BroadcastReceiver but I don't understand any of the guides...I mean am I supposed to set the intent that the alarm manager does to the BroadcastReciever? Or can I just start up the app and then the alarm exists again or what? How are the alarms stored in android?
But it doesn't work when the emulator is turned off and on again.
That is the correct behavior -- AlarmManager's schedule is cleared on a reboot. You need to specifically register to receive the ACTION_BOOT_COMPLETED broadcast, in order to re-establish your alarm events after a reboot.
I mean am I supposed to set the intent that the alarm manager does to the BroadcastReciever?
Well, if you are using a _WAKEUP-style alarm, the recipe is to use a getBroadcast() PendingIntent with AlarmManager, where the BroadcastReceiver is either a WakefulBroadcastReceiver (and follows those instructions) or passes control to my WakefulIntentService.
I have somewhat-contrived examples of using WakefulIntentService and WakefulBroadcastRecevier.
How are the alarms stored in android?
AFAIK, they are held in the memory of a core OS process and are not persisted.
Let's say my app sets a repeating alarm (that repeats every day) using the code below:
myAlarmMgrObj.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, myPendingIntent);
What will happen to this pre-set repeating alarm when the user uninstalls my app? Because I have no handle on uninstall, I have no way of knowing when to cancel it.
I am guessing that because there will no longer be a Broadcast Receiver to receive my PendingIntent, the PendingIntent will not start its lifecycle at all. But does this mean that every day, the broadcast will be sent out anyway?
What will happen to this pre-set repeating alarm when the user uninstalls my app?
It is the OS's responsibility to cancel the alarm.