Alarm and Reminder in Windows Phone 7 (Mango)


Introduction

One of the coolest features in the new Mango (7.1) update is the ability of applications to do background tasks (finally!). Two of the new features brought about by this are the Alarm and Reminder classes. They essentially give you, the developer, access to use OS level alarms, and calendar-like reminders. All of this lives under the Microsoft.Phone.Scheduler namespace, which I suggest you check out if you haven't already. Anyways, on with some demos!

Alarm

AlarmWP7

As you can see, this alarm looks exactly like the system one. I've annotated the above picture to show what you can and cannot change in the dialog. Now let's look at the code for creating this.

public void setAlarm()
{
      
if (ScheduledActionService.Find("My Alarm") != null)
              ScheduledActionService.Remove(
"My Alarm");

       Alarm a = new Alarm("My Alarm")
       {
              Content =
"Alarm",
              BeginTime = DateTime.Now.AddMinutes(1)
       };
       ScheduledActionService.Add(a);
}


This function is very simple and just shows the lifecycle of using an alarm repeatedly in your application. When creating an Alarm (or Reminder) you give it a name. This is so you can identify what has or has not triggered or whatever other logic you want to perform. These names are also unique, so you can't add two alarms with the same name. The first section of code makes sure that our alarm isn't still in the Scheduler, but if it is, removes it. We then make out alarm, give it some info; Content, as shown in the picture, and BeginTime, which is when your alarm should go off, represented as a DateTime object. The last step is to add the alarm to the Scheduler queue.

One thing to note is that while you can see the Title property of the Alarm object, setting it will result in an error. If you want to change the Title, keep reading to look at Reminders.

Reminder

ReminWP7

The Reminder is a little more customizable than the Alarm, as you can change the title. Also, if a user taps a Reminder, it will take them to your application; the Alarm does not do that. Creating one is very similar, as shown below.

public void setReminder()

{
       if (ScheduledActionService.Find("My Reminder") != null)
              ScheduledActionService.Remove("My Reminder");
       Reminder r = new Reminder("My Reminder")
       {
              Content = "Remind Me",
              BeginTime = DateTime.Now.AddMinutes(1),
              Title = "My Title"
       };
       ScheduledActionService.Add(r); 
}
 

This code is pretty much identical to our Alarm, except that now we can set the Title. There is an unused property here, NavigationUri, which allows you to customize the information passed to your application when the user taps the Reminder, which is beyond the scope of this simple introduction.
 
Conclusion
 I hope this brief introduction has helped you understand these two new features and if it brought up any questions, feel free to ask in the comments.


Similar Articles