Android Alarmmanager - Scheduling A Recurring Intent To Fire Off Twice A Day
After reading lots of sample code into this matter, I'm trying to figure out the simplest way to achieve the following: I want to be able to schedule an Intent that calls back to m
Solution 1:
That seems alright to me, however instead of creating two calendar objects you could just set your interval to
AlarmManager.INTERVAL_DAY/2
unless your intents are doing different things.
Also,
alarms.cancel(firstCallIntent);
alarms.cancel(secondCallIntent);
should be enough to cancel all alarms of those types, no need for:
firstCallIntent.cancel();
Edit: Setting 2 calendar objects
//midday
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 12);
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);
//7pm
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 19);
cal2.set(Calendar.MINUTE, 00);
cal2.set(Calendar.SECOND, 00);
Calendar.getInstance() will return a calendar object and set it to the current system time. Each .set method changes a certain variable of that calendar object. So currently if it was 8pm, it would set the alarm for 12 and 7 that day, which would be no use. So if you want to set it for the next day, you'll need to use cal1.add(Calendar.DAY_OF_MONTH, 01); to add an extra day, setting for that time the next day. Hope this helps.
Post a Comment for "Android Alarmmanager - Scheduling A Recurring Intent To Fire Off Twice A Day"