I need to schedule a certain repetition alarm, that can happen any day of the week, several days. It can be on Monday, or every weekday, or only the weekend days- and of course this needs to be stored in the DB.
My question is, how could I store this in the DB? I thought of using an int array of seven positions, and depending on whether which position is a true (or 1) I'll assume there's an alarm that way.
Any other ideas? Suggestions please?
Thanks a lot in advance.
You could store a single byte
to represent your week and pull values out of it using a bitwise &
operation. Bit 1 could represent Monday, Bit 2 Tuesday, etc. You can use this to represent all combinations of days, e.g:
01100000 - Saturday/Sunday (Weekend)
01110001 - Friday/Saturday/Sunday/Monday (Long weekend)
When reading the value you would use something like:
byte val = 0x71; //01110001
boolean mondayActive = (val & 0x1) == 1;
boolean fridayActive = (val >> 4& 0x1) == 1;
EDIT comment
This assumes that you are already familiar with the AlarmManager and were looking for a mechanism to track your alarms as you cannot use a single alarm to schedule events in the manner the OP described. If you need to mimic cron in a single task, possibly take a look at something like the BuzzBox SDK.
EDIT write sample
public static final int MONDAY = 0x01; //00000001
public static final int TUESDAY = 0x02; //00000010
public static final int WEDNESDAY = 0x04; //00000100
public static final int THURSDAY = 0x08; //00001000
public static final int FRIDAY = 0x10; //00010000
public static final int SATURDAY = 0x20; //00100000
public static final int SUNDAY = 0x40; //01000000
//example values to write
int weekend = SATURDAY | SUNDAY; //01100000
int longWeekend = FRIDAY | SATURDAY | SUNDAY | MONDAY; //01110001
//and as per flightplanner's comment, to read
boolean mondayActive = (weekend & MONDAY) == MONDAY; //false
mondayActive = (longWeekend & MONDAY) == MONDAY; //true