Calculating 3 days for alarm manager trigger - android

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.

Related

Why is CountDownTimer not accurate

I have 2 CountDownTimer objects in my app, that count the time to next event constantly as the app is running.
So today I debugged it, and found out that the timer is not accurate at all, I have given it an interval of 1 hour, starting at 9:00 am, and now 14:04 was when the next event fired. whole 4 mins late, and it is getting more and more with each event.
So should I use the CountDownTimer for such task, or I should just make an if check in it's onTick() method, where every second I will check for fixed time passed since the previous event, then reset the time for after 1 hour?
I can't do these tasks with AlarmManager, since all these things are happening in foreground service , and as much as I know, Alarms will be killed if the users swipes the app away from the running apps menu

Alarm when device hasn't been touched by user in x hours?

Is there a way to get an alarm to fire some time after the user has last touched the device? i.e. when the device has been in the users pocket for a while, or when the user is sleeping.
I need to do a db-locking update for 2 minutes every few days, preferably when the user doesn't want to use the app.
Edit:
I have no clue when the user will not be using the app, since it's something that will be used anytime during the day, just before going to bed and just after waking up.
Currently I'm just picking a random time between 07:00 and 08:00 every 3 days (using AlarmManager, restarting on boot, etc), hoping that the user doesn't really need this feature at that time of day, but I'm pretty sure someone will.

how to sense that the scheduled time by AlarmManager was passed in rebooting

for example,An AlarmManager to count numbers at 7AM using Calendar was set.
But user turns off the android at 6am and turns on 8am.
So program does'nt count.
I dont think i can avoid this situation if i reset AlarmManager on receiver of BOOT COMPLETED.
Please tell me the way to count exact number in this situation.
You'd have to do a bit of work. Save the time of your next alarm in permanent storage (file, shared preference, or database). Whenever an alarm occurs, update this value. Set a BOOT_COMPLETED listener. When the boot completed listener launches, have it get the current time and check if its later than the time of the next alarm you stored. If so, you missed it. If not, you're ok.
Now if you have to worry about missing multiple alarms, it gets more complicated, but the idea is the same.

alarm triggers even after finish time in android

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

Set repeating alarm once a month

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?

Categories

Resources