Getting Started with Windows Phone Alarms
published on: 10/31/2011 | Views: N/A | Tags: Mango
by WindowsPhoneGeek
In this article I am going to talk about how to use Alarms in Windows Phone applications.
Basically an Alarm is one of the two types of scheduled actions that are available in windows Phone Mango It allows you to specify a sound file to play when the notification is launched. A scheduled notification is a dialog box that pops up on the screen at a specified time, similar to the notifications displayed by the phone's built-in applications.The dialog box displays some customizable text information to the user and allows the user to dismiss the notification or postpone it until later. If the user taps the notification, the application that created it is launched.
For reference take a look at the MSDN documentation.
NOTE: Note that the schedules for these notifications are accurate only within a range of one minute. In other words, the notification can be launched up to one minute after it was scheduled.
Getting Started
Step1: Create a Windows Phone 7 application project.
Step2: Add a new page and name it "NewAlarmPage" and add the following code inside NewAlarmPage.xaml:
<StackPanel Grid.Row="1" Orientation="Vertical">
<!--<TextBlock Text="Title:" />
<TextBox x:Name="txtTitle" Text="alarm title" />-->
<TextBlock Text="Content:" />
<TextBox x:Name="txtContent" Text="alarm content" />
<TextBlock Text="Seconds to start:" />
<TextBox x:Name="txtSeconds" Text="10" />
<StackPanel Orientation="Horizontal">
<Button x:Name="btnDone" Content="done" Click="btnDone_Click" />
<Button x:Name="btnCancel" Content="cancel" Click="btnCancel_Click" />
</StackPanel>
</StackPanel>
Step3: Add the following code inside NewAlarmPage.cs.
NOTE: In the below example we use Guid.NewGuid().ToString(); since alarm names must be unique .
NOTE: The value of BeginTime must be after the current time. Set the BeginTime time property in order to specify when the alarm should be shown.
NOTE: ExpirationTime must be after BeginTime .The value of the ExpirationTime property specifies when the schedule of the alarm expires. This is very useful for recurring alarms, ex: show alarm every day at 5PM but stop after 10 days from now.
NOTE: Do not forget to call this.NavigationService.GoBack(); so that the app will navigate to the main page.
private void btnDone_Click(object sender, RoutedEventArgs e)
{
string alarmName = Guid.NewGuid().ToString();
// use guid for alarm name, since alarm names must be unique
Alarm alarm = new Alarm(alarmName);
// NOTE: setting the Title property is not supported for alarms
//alarm.Title = this.txtTitle.Text;
alarm.Content = this.txtContent.Text;
double seconds = 10.0;
double.TryParse(this.txtSeconds.Text, out seconds);
//NOTE: the value of BeginTime must be after the current time
//set the BeginTime time property in order to specify when the alarm should be shown
alarm.BeginTime = DateTime.Now.AddSeconds(seconds);
// NOTE: ExpirationTime must be after BeginTime
// the value of the ExpirationTime property specifies when the schedule of the alarm expires
// very useful for recurring alarms, ex:
// show alarm every day at 5PM but stop after 10 days from now
alarm.ExpirationTime = alarm.BeginTime.AddSeconds(5.0);
alarm.RecurrenceType = RecurrenceInterval.None;
ScheduledActionService.Add(alarm);
this.NavigationService.GoBack();
}
Step4: Add the following code in order to handle the Click event of the Cancel button:
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.GoBack();
}
Step5: Add the following code inside MainPage.xaml to display a list of alarms:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox x:Name="lbAlarms" Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding Content}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding BeginTime}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Button x:Name="btnAddNewAlarm" Content="Add New" Click="btnAddNewAlarm_Click" />
<Button x:Name="btnRemoveSelectedAlarm" Content="Remove Selected" Click="btnRemoveSelectedAlarm_Click" />
</StackPanel>
</Grid>
Step6: In MainPage.cs, implement a RefreshAlarmList method that we will call to refresh the list of alarms
private void RefreshAlarmList()
{
IEnumerable<Alarm> alarms = ScheduledActionService.GetActions<Alarm>();
this.lbAlarms.ItemsSource = alarms;
}
Step7: Handle the Click event of the "Add New" button to implement navigation to the NewAlarmPage.xaml page
private void btnAddNewAlarm_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("/NewAlarmPage.xaml", UriKind.Relative));
}
Step8: Handle the Click event of the "Remove Selected" button to implement removing of alarms
private void btnRemoveSelectedAlarm_Click(object sender, RoutedEventArgs e)
{
Alarm selectedAlarm = this.lbAlarms.SelectedItem as Alarm;
if (selectedAlarm != null)
{
ScheduledActionService.Remove(selectedAlarm.Name);
this.RefreshAlarmList();
}
}
Step9: Finally in MainPage.cs, override the OnNavigatedTo method and call RefreshAlarmList to refresh the list of alarms when the user navigates to MainPage.xaml
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
this.RefreshAlarmList();
}
That was all about getting started with Windows Phone alarms. You can find the full source code here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Nice
posted by: mdiallo on 11/17/2011 1:00:58 AM
Thanks for the example
Multiple Selection removal
posted by: Laurent on 12/13/2011 3:16:11 PM
I have a multiple selection mode in my listbox and I want to be able to remove multiple alarms at one time. I tested a lot of different things but nothing works.
In my xaml, I have:
<ListBox x:Name="lbAlarms" Grid.Row="0" Height="297" VerticalAlignment="Top" Margin="20,192,-20,0" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" SelectionMode="Multiple">
Any ideas of how to handle this?
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
