Windows Phone 7.1 SDK is Alarming

August 29, 2011
No Comments.


Ok, maybe I like my distracting titles…my apologies.

As many Windows Phone developers have noticed, Mango (e.g Windows Phone SDK 7.1) supports background processing through something called Agents. While Agents are certainly a welcome addition, I am exceptionally impressed in the fact that Mango also supports a bunch of features to avoid having to have background processing agents. In this post, I’ll show you one of these: Alarms.

The general idea of Alarms is being able to create application specific alarms (not just using the Alarm clock app). Alarms do come with a couple of small caveats:

  • Alarms are fired once per minute. Sub-minute alarms will round to the minute.
  • Alarms shown over lock screen cannot launch your application.
  • Cannot control how long an alarm is snoozed.
  • Alarm’s Title is always “Alarm”.
  • Can only launch the application, not a deep link to a specific page.

To compliment alarms, the phone also supports Reminders which can overcome some of these limitations, but I’ll show them in a future post.

With that in mind, you can create your own alarms and even specify the type of alarm sound that is played. To do this, create an instance of the Alarm class (in the System.Phone.Scheduler namespace):

// Alarm Name (unique per app)
var alarmName = Guid.NewGuid().ToString();

// Create the Alarm
var alarm = new Alarm(alarmName)
  // When the Alarm should sound
  BeginTime = DateTime.Now.AddMinutes(1),

  // The Message in the Alarm
  Content = "Mango Alarm",

  // Exists on Alarms, but it not used
  //Title = "Not used!",

  // What sound to play for the alarm
  Sound = new Uri("alarm.wav", UriKind.Relative)

Each alarm must be named (unique, per application) so you can look it up later (if you want to disable it or remove it). BeginTime when the alarm should fire. **Content **is the message shown in the UI. Finally, Sound allows you to specify a sound file (mp3, wma or wav) to be played. This file must be delivered with your .xap file (can’t be in isolated storage or via an Internet connection). While the **Title **property exists on the Alarm class (because of the base class), it will throw an exception if set to ensure that you don’t think the Title will be used.

Once you create the alarm, you can register it like so:

// Add the Alarm to the Phone

The ScheduledActionService is a class that is used to register reminders, alarms and agents. The Add method simply adds the alarm to the phone. The other methods of the service are also supported by the Alarms as well (e.g. **Find, Remove, Replace **and GetActions). There you go, you can now add your own alarms!