Android alarm synced with clock - android

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.

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.

Special alarm application

I need an alarm application for specific time of every hour. For example, if I set the alarm value at 25, the alarm will go off at 25th min of every hour.ie 11:25am, 12:25pm, 1:25pm, 2:25pm etc. Is it possible? or is there an application, preferably android that has such functionality?
yes, it is possible with repeating Alarms. you can set alarm to fire at exact time with setRepeating() and the alarm's interval set to every hour and RTC_WAKEUP alarm type(Wakes up the device to fire the pending intent at the specified time). there are some examples in the first link.

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?

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