alarm triggers even after finish time in android - 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

Related

Should I use setAlarm, setExact or setRepeat Alarm?

For setAlarm() parameter : Should I use SystemClock.elapsedRealtime(), calander,getTimemillise or something?
Because Alarm is coming fine, but when I manually go to my Android phone date & time settings and change the time (not past, but future time), alarm is not coming and triggers immediately.
This is an intended behaviour as specified in the official Alarm Manager docs for all the set methods.
If the stated trigger time is in the past, the alarm will be triggered immediately.
to manage you can try to cancel such alarms with past trigger time in your reciever .
How to choose between
System.currentTimeMillis() vs SystemClock.elapsedRealtime()
As the link specifies the :-
The first method, will give you the standard "wall" clock (time and date) expressing milliseconds and is affected by and changes in time and date from the settings.
While the second method, return the time in milliseconds since the system was booted, and including sleep time. Hence would not be affected with the changes in the settings so it is the recommend basis for general purpose interval timing.
So if you do not want the time to be dependent on the time-zonal changes , it is better to use the second method.Otherwise you need to handle it as mentioned before.
As compared to other alternatives like Date or Calender , it is better to use currentTimeMillis() due to performance benefits.

Stratagy to set repeat days alarm in android

I'm really confused about which technique I should use to set and cancel the repeat days alarm.
I have two conditions in my mind.
First is that I set all the repeating days alarm at once when alarm is added.
Second is that I should check at the time when alarm is triggered that is there any alarm for next day or not.
Problems with my conditions.
If i set the repeating days at once and if the user changed the repeating days like user added alarm at 8:30 AM for Monday , Wednesday and Friday and then the user changed the days to Monday and Friday only then how can i keep check on it and cancel it for Wednesday.
In second condition if I check at the time when alarm goes-off for the next day and what if user did not set that alarm for next day and set that same alarm for day after tomorrow? Like user set alarm at Monday 8:30 AM and the same alarm is also set for Wednesday not Tuesday then how can i check this ?
It would be great pleasure if anybody can give me some kind of solution to keep check on it.
You can start from Clock app of AOSP https://android.googlesource.com/platform/packages/apps/DeskClock/+/master
In the specific check how the alarms are implemented in the Alarm class.
In any case you have to schedule the alarm for the "first next day" that is setted, then when the alarm is fired, you need a method that tell you when is the next occurrence.
If an user need to modify the alarm you have to unschedule the existing alarm and reschedule it.
You should also save your alarm to a database because if the device is rebooted all scheduled alarms are lost. So you have to register a boot receiver and re-schedule all the alarms

Android alarm synced with clock

Is there a way to tack the Alarm to the minute (or hour etc.?)
I know how to set an alarm that runs every n milliseconds, but I'm unclear on how to set it so that it's tacked to the clock tick... meaning, I want to set an alarm for every 120,000ms (every other minute) but I want it invoked ON the minute (not somewhere in between minutes).
You would want to use the RTC_WAKEUP option when creating an Alarm. Here is the related document describing this process.

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?

Past Alarm Trigger

Will the alarm trigger if the trigger Time is set to past Time?
For example, if the current time is 17:00 PM and if I set the alarm as 15:00 which is already passed, will the alarm be triggered?
No it won't.
However, if you set it at 17:00, but have your mobile shut down at that moment, and later start up your mobile at lets say 17:05, then you should get a postponed alarm.

Categories

Resources