noloman
noloman

Reputation: 11975

Repetition alarm in Android

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.

Upvotes: 0

Views: 328

Answers (4)

berserk
berserk

Reputation: 2728

In addition to @JohnMark13's answer and @noloman's question "how would I modify then the values? ", I would like to add this.

To modify values, you can use bitwise operatoes. I will explain different cases:

To enable a day: Suppose you want to add monday to an alarm. Take a number 0x00000001 and OR it with your current value (i.e. a|b). The value returned will have the Monday enabled. Similar masks can be made for rest of days depending on their bit location. For weekends, use 0x01100000.

To disable a day: Suppose you want to remove monday from an alarm. Take a number 0x01111110 and AND it with your current value (i.e. a&b). The value returned will have the Monday disabled. Similar masks can be made for rest of days depending on their bit location. For weekends, use 0x00011111.

To switch previous value of a day: The mask for Monday is 0x00000001. If you XOR the value with this mask (a^b), it will reverse the bit of monday from the value, hence add/remove it depending on previous value. For weekends, the mask will be 0x01100000.

Hope that helps.

Upvotes: 0

JohnMark13
JohnMark13

Reputation: 3749

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

Upvotes: 2

Erik
Erik

Reputation: 5119

follow up on Diego answer You typically use the AlarmManager for this. Create an intent that will wake up the below AlarmReceiver.class that is a BroadcastReceiver. AlarmManager is firing of this intent at your dateForAlarm. The dateForAlarm can be a String in the DB like "2009-04-14 15:16:45"

When device is restarted you need to recreate all alarms and use another or same BroadcastReceiver to get all dateForAlarm from DB and create the alarms.

put this in your manifest to capture a restart signal to your BroadcastReceiver

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Code:

Calendar dateForAlarm = "yyyy-MM-dd HH:mm:ss" // example date.."2009-04-14 15:16:45"
Intent intent = new Intent(context, MyBroadcastReceiver.class);
intent.putExtra(WAKE_UP_AND_ALARM_NOW, "value you want to send to MyBroadcastReceiver like "god morning"");
PendingIntent sender = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// Get the AlarmManager service 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, dateForAlarm, sender);

Upvotes: 0

Diego Palomar
Diego Palomar

Reputation: 7061

AlarmManager provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

Here you have a tutorial: Tutorial on Android AlarmManager

Upvotes: 0

Related Questions