I am using alarm manager along with broadcastReceiver. I am able to set an alarm and all that. But I am stuck with how to approach my problem. I need to be able to set a repeating alarm and the trick here is to have it repeat every 14th of a month at 4:00 pm (so monthly alarm).
How do I go about it? I know how to make it repeating every day or every week as it is easy to calculate the how many milli seconds in a week, but when we are talking monthly, every month has different number of days so I can't set it with a fixed interval.
Any help here?
Thank you
Based on the req. you provided above, I would only schedule one alarm at a time, just calculate the new time values when the previous one is triggered. One thing you will want to do as well is setup a service to listen for boot complete event. Alarms do not persist through restarts. you can find information on how to do that here How to start an Application on startup?
Related
I'm trying to make a feature where user can select the time frame and interval in which he would receive notifications to his phone. I'm using AlarmManager for repeating notifications, however it only covers the start time of notifications and the interval/frequency of notifications.
How could I make the notifications go off only from user selected hours, let's say every day 1 PM - 8 PM (13:00 - 20:00) every 2 hours?
I was thinking about using WorkManager to start AlarmManager at selected start hour and to cancel AlarmManager on user selected end hour, that way WorkManager would handle the everyday start/end hours and AlarmManager would send a notification with provided frequency. But I'm not sure if it's a "correct" approach.
So, I think what you are proposing would work, but another option is to us a non-repeating alarm that sets the next alarm according to the users desired schedule that is persisted some how (e.g. shared preferences).
I want to build notifications that are showed recurrently in specific days of the week, for example, I want X notification every monday and friday of each week. I know how to set intervals with alarms, but not how to select specific days
You can set repeating alarm for every day and show notification only those days that are needed on onReceive() function of the alarm BroadcastReceiver.
The logical way to control push notification timing is from server side but if you want to do it on your phone you must work with android os date and time and Java CALENDAR class.
I am creating small Alarm project which has week days option. (I am using alarm manager).
The alarm triggers perfectly on given time and day but the problem is that it also triggers any previous alarm if I change the day or time after to the given alarm time.
For eg.
If the alarm is set to ring at 5:00 AM Monday Wednesday Friday. - And the current time is 6:00PM Sunday.
And another alarm set for for 4:00PM Monday Wednesday Friday - The current time is 6:30PM Sunday.
For testing I changed the day to Tuesday 6:00PM - Immediately the two alarm triggers one by one for Monday's schedule.
How do I stop this specific alarm and trigger only next Monday rather immediately? Do I need to check the dates also???
Let me know!
You can't really. The alarm manager bases everything off of the system time. If you change the system time manually it is going to act just as if all the time had passed between now and whenever you set it for.
Edit: You can find the source code for the stock alarm application. Here is a link to one of the relavent objects Alarms.
I didn't test it any to ensure but from what I can tell they are storing the time the event is supposed to fire, and if it suddenly finds that it is in the past it ignores the alarm instead of letting it trigger. Some other relavent classes to check out are SetAlarm and AlarmClock, search for them on grepcode if you want to follow the way they set and react to the alarms in the stock app.
But it is still important to point out that manually changing the system time can still be used to manipulate the alarms. If for instance you set an alarm for 10:00pm tomorrow, then manually jump the time to 9:59pm tomorrow. The alarm will fire after 1 minute.
compare your alarm time with System.currentTimeMillis()
if your alarm time is smaller then the current time, than no need to set the alarm
I want to set a repeating alarm monthly; but my app resets the alarm everytime it boots up. So is it bad practice to do it this way rather than setting a repeating alarm?
(The Alarm is set for a specific day of the month. (e.g. The 8th, 16th, 21st, etc). So if it is past that day, it simply +1's on the total the current month.)
I also run a service on boot up to set any alarms so even if they don't go into the app for a month, it will be reset again.
Again, is this the best way of doing this or is this bad practice?
So is it bad practice to do it this way rather than setting a repeating alarm?
If it works for you, it is probably fine.
I also run a service on boot up to set any alarms so even if they don't go into the app for a month, it will be reset again.
That is a little odd. Getting control at boot time to re-establish alarms is fine, but you should not need a service for that.
ok so I have an alarm manager that I have to go off every three days, when it goes off I write the current system time to shared preferences so that if the phone is turned off I have when it was last fired.
my question is how do I calculate 3 days from when it was last fired after the phone has been turned off since I can only write to shared preferences when its fired?
I was thinking something like this
long refresh = lastTime + (360000*24)*3;
where lastTime is when it was last fired but if the phone was restarted between then wouldnt it be another 3 days from that restart or am i thinking this wrong?
You have a zero missing (there are 86400000 milliseconds in a day), but other than that your approach looks fine (assuming that lastTime is just a stored value from System.currentTimeMillis()). AlarmManager uses absolute times for the trigger time.
If the phone has been turned off for more than 3 days then when you restore the alarm it will be overdue and will fire immediately. One thing to be aware of is that if you restore a repeating alarm in this way and it's been 5 days since it last fired, then when you restore it using a date in the past it will fire immediately (the overdue day 3 alarm) and then fire again after one more day (the day 6 alarm). You may want to adjust for this.