I want to execute some task (e.g. show notification), by schedule, e.g.: 10:00 every day AND 11:00 every monday and etc., I can't use AlarmManager, because the time interval between tasks is different and I can't run task every N second. Can I specify a few tasks for AlarmManager? How I can do it and what I should use?
I can't use AlarmManager, because the time interval between tasks is different
Yes, you can.
One option is to use different alarms for different tasks. In your example, you would have one alarm for "10:00 every day" and a separate alarm for "11:00 every Monday".
Another option is to use a single alarm for whatever comes next, where it then does whatever your business logic is (e.g., remind the user of the event) and then schedules the next alarm for the next thing to occur. For example, using my time zone, it is 07:45 on a Saturday right now. Your app would have an AlarmManager alarm to get control at 10:00 ("10:00 every day"). When that alarm is invoked, it would then schedule an AlarmManager alarm for 10:00 Sunday ("10:00 every day"). 10:00 on Sunday, it would then schedule an alarm for 10:00 Monday ("10:00 every day"). 10:00 on Monday, it would then schedule an alarm for 11:00 Monday ("11:00 every monday"). 11:00 on Monday, it would then schedule an alarm for 10:00 Tuesday. And so on.
If there are only a couple of rules, I would go with the first option. If there could be N possible rules, I would go with the second option, so as not to flood AlarmManager.
Can I specify a few tasks for AlarmManager?
Yes. Make sure that the PendingIntent objects are distinct. The easiest way to do that is to use unique values for the requestCode parameter to PendingIntent factory methods like getBroadcast().
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.
This is strange, what is the reason method setInexactRepeating trigger approximately at given time rather then exact time.
I need to trigger alarm on every day basis at one particular time, i have said
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger time to 8:30 a.m.
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
I have set time 8:30 am in the calendar, when i shift device day to next day, alarm get trigger immediately even before i shift device time to 8:30am.
What is happening here, why i can't make exact trigger using this api, if any of you have experience this, could you share your experience. Thanks!
According to documentation (here) there is text:
As described above, choosing the alarm type is often the first step in
creating an alarm. A further distinction is how precise you need your
alarm to be. For most apps, setInexactRepeating() is the right choice.
When you use this method, Android synchronizes multiple inexact
repeating alarms and fires them at the same time. This reduces the
drain on the battery.
For the rare app that has rigid time requirements—for example, the
alarm needs to fire precisely at 8:30 a.m., and every hour on the hour
thereafter—use setRepeating(). But you should avoid using exact alarms
if possible.
With setInexactRepeating(), you can't specify a custom interval the
way you can with setRepeating(). You have to use one of the interval
constants, such as INTERVAL_FIFTEEN_MINUTES, INTERVAL_DAY, and so on.
I hope it could help You bit. There is also small example for it in previous link I gave You.
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
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