Windows Phone 7.1 now supports scheduled tasks which runs even the application is not running. We can now schedule a task and when the bell hits, it starts executing the said task. In this chapter of the tutorial series, we will explore this using the Alarm class.

 

Alarm class creates an instance which runs in the background using the task agent and at the specified time, it will raise the event to execute. If your application is not running at that time too, it will execute and show the alarm screen. Read to know more about the class with a small demonstration.

 

Index - Windows Phone 7 (Mango) Tutorial

 

Know about the Alarm Class

Before going to the deep, let us first explore the structure of the Alarm class and the various properties of it. You can find the Alarm class under the "Microsoft.Phone.Scheduler" namespace which inherits from the ScheduledNotification class. The base class of them is the ScheduledAction class. Both of these classes are present in the same namespace.

 

ScheduledAction consists of some basic properties like "Name", "BeginTime", "ExpirationTime", "IsEnabled" and "IsScheduled".  IsEnabled and IsScheduled properties are readonly that means, you cannot change their values explicitly.

 

ScheduledNotification inherits from ScheduledAction and exposes three more properties called "Content", "RecurrenceType" and "Title". RecurrenceType property is a enum type of RecurrenceInterval which indicates whether the task will run daily, weekly, monthly, yearly or end of each month.

 

The Alarm class inherits from ScheduledNotification and exposes new property called "Sound". As it is a type of Uri, you can set the url of the sound file which will play when the alarm raises. It also exposes another property called "Title" which overrides the actual implementation of the base class.

 

Find the diagram of the class structure here. This will give you much clear visibility.

 

WP7.1 Demo - Class Structure for Alarm

 

Hope this gave you the basic understanding of the class structure before we jump start on the code. Now we will create a small demo app to show you how to use the Alarm window.

 

Create the Basic Application UI

We will make this step as simple as possible by adding a TextBox where the user can enter the time in second. The alarm window will show up on the screen after that duration. We will add two buttons, one will start the alarm and the other will reset it.

 

Here is our basic UI for demonstrating the example:

 
<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Center">
        <TextBlock Text="Set Time (Sec) : " VerticalAlignment="Center"/>
        <TextBox x:Name="txtTime" Width="150"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Center">
        <Button Content="Set Alarm" Width="200" Height="70" Click="SetAlarm_Click"/>
        <Button Content="Reset Alarm" Width="200" Height="70" Click="ResetAlarm_Click"/>
    </StackPanel>
</StackPanel>

 

Implement the Alarm Feature in Code

Now we will set the Alarm time and content from code behind on click of the "Set Alarm' button. We will create a method called "CreateAlarm". This will create the instance of the Alarm by passing a name to the constructor. Then we will add other properties to it like "Content" and "BeginTime" as shown in the below code snippet:

 
private static void CreateAlarm(double time)
{
    var alarm = new Alarm("MyAlarm")
                    {
                        Content = "You have a meeting with your team now.", 
                        BeginTime = DateTime.Now.AddSeconds(time)
                    };
 
    ScheduledActionService.Add(alarm);
}
 
private static void ResetAlarm()
{
    ScheduledActionService.Remove("MyAlarm");
}

 

 

Once we create the instance with the required property values, we need to add it the the ScheduledActionService. This will place the task in the background thread and will execute even the application has been closed.

 

The ResetAlarm() method will remove the said alarm from the ScheduledActionService by passing the name of the task.

 

Points to be Noted Down

  1. After execution of the task you need to remove it from the service else it will throw exception.
  2. As it is just a demonstration of the Alarm task, you need to click "Reset" button every time before clicking the Set Alarm from the second time onwards.
  3. You cannot set the the Title of the Alarm Window. Setting it explicitly will throw "NotSupportedException" as shown below:

WP7.1 Demo - Setting Alarm Title throws NotSupportedException

 

Demo

It's the demo time now. Build your project and run the application inside the emulator. You will see the UI of our application as shown in the first screenshot. In the textbox, enter the desired time (in second) and click "Set Alarm" button. This will add the task to the service and after the specific period interval it will popup the Alarm window on the screen with the proper message that we set as the content. It will have two buttons named snooze and dismiss to do the proper action with the dialog.

 

WP7.1 Demo - Setting Alarm Properties     WP7.1 Demo - Alarm Screen

 

Also if you set the time and exit from the application by pressing the Start hardware button, it will also execute the alarm task from the background and popup the same message so that, you will not miss any of them.

 

Hope this was helpful to you to understand the basic of the alarming feature in Windows Phone 7. Stay tuned for my next chapters on the background tasks.

 

Download Source Code

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.