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.
Related
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.
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
I have an Android alarmManager being set and a broadcastReceiver to pick it up, whenever the app triggers onResume, I am forcing a new alarm to be set.
So I'm wondering for best practices and considering good resource management, should I be removing the first alarm (if it hasn't triggered) before I reset a new alarm ?
Potentially if an alarm doesn't trigger, each time the user re-opens the app, is it creating more resource usage even if the alarm is for the same time ?
Yes you should cancel it when it is appropriate.
I suspect you will have a lot of wasted cycles otherwise. For Example,
App opens at Time T.
Schedule an alarm for T'.
Next the user opens the app again at some time, X seconds prior to T'.
You will then schedule an alarm presumable at another time T''.
However you will get an alarm firing in X secs anyways. If you ignore it, you are simply wasting battery if the phone had to wake in order to deliver the alarm.
If you create the same PendingIntent for your alarm, then the previous one will automatically be canceled when you set it again.
See AlarmManager.set(), PendingIntent.
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.
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