與眾不同 windows phone (11) - Background Task(后台任務)之警報(Alarm)和提醒(Reminder)
作者:webabcd
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之后台任務
- Alarm - 警報
- Reminder - 提醒
示例
1、演示 Alarm(按一個時間計划彈出警報信息)
AlarmDemo.xaml
<phone:PhoneApplicationPage x:Class="Demo.BackgroundTask.AlarmDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <StackPanel Orientation="Vertical"> <TextBlock Text="Alarm 的樣式" /> <Image Source="/BackgroundTask/Alarm.png" /> <Button x:Name="btnRegister" Content="注冊一個一分鍾后啟動的 Alarm" Click="btnRegister_Click" /> <TextBlock x:Name="lblMsg" /> </StackPanel> </phone:PhoneApplicationPage>
AlarmDemo.xaml.cs
/* * ScheduledAction - 所有計划活動的基類,抽象類。ScheduledNotification 和 ScheduledTask 繼承自此類 * ScheduledNotification - 用於按時間計划彈出信息,抽象類 * * Alarm - 按一個時間計划彈出警報信息,每一個程序在某個時刻最多只能有 50 個警報信息。Alarm 繼承自 ScheduledNotification * Name - Alarm 的名稱,此名稱即 ID * Title - 警報的標題,這個只能顯示系統默認值,無法修改 * Content - 警報的詳細內容 * Sound - 警報的警報音的地址(Uri 類型) * BeginTime - 在此時間點彈出警報信息(系統每隔一分鍾會統一調度所有 ScheduledNotification 一次,也就是說系統會在 BeginTime 所指定時間點的一分鍾之內彈出相關信息) * ExpirationTime - 警報的過期時間。當彈出警報警報后,如果用戶選擇了“推遲”,則一段時間過后還會繼續彈出此次計划的警報信息,但是在此值所指定的時間點過后則永遠不再彈出此次計划的信息 * RecurrenceType - 彈出信息的時間計划類型。Microsoft.Phone.Scheduler.RecurrenceInterval 枚舉:None|Daily|Weekly|Monthly|EndOfMonth|Yearly * IsEnabled - 目前此值無用 * IsScheduled - 此 ScheduledAction 之后是否有執行計划(只讀字段) * * ScheduledActionService - 管理 ScheduledAction 的類 * ScheduledActionService.GetActions<T>() where T : ScheduledAction - 查找系統中已注冊的 ScheduledAction 類型的數據 * ScheduledAction.Find(string name) - 按名稱查找指定的 ScheduledAction * ScheduledAction.Remove(string name) - 按名稱刪除指定的 ScheduledAction * ScheduledAction.Add(ScheduledAction action) - 注冊一個新的 ScheduledAction * ScheduledAction.Replace(ScheduledAction action) - 更新指定的 ScheduledAction */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.Scheduler; namespace Demo.BackgroundTask { public partial class AlarmDemo : PhoneApplicationPage { public AlarmDemo() { InitializeComponent(); this.Loaded += new RoutedEventHandler(AlarmDemo_Loaded); } void AlarmDemo_Loaded(object sender, RoutedEventArgs e) { ShowRegisteredAlarm(); } // 顯示程序中已有的 Alarm private void ShowRegisteredAlarm() { // IEnumerable<ScheduledNotification> notifications = ScheduledActionService.GetActions<ScheduledNotification>(); IEnumerable<Alarm> alarms = ScheduledActionService.GetActions<Alarm>(); lblMsg.Text = "程序中已注冊的 Alarm 的名稱為:" + string.Join(",", alarms.Select(p => p.Name).ToList()); } private void btnRegister_Click(object sender, RoutedEventArgs e) { // 查找程序中指定的 Alarm,如果沒有則實例化一個 Alarm alarm = ScheduledActionService.Find("alarm") as Alarm; if (alarm == null) alarm = new Alarm("alarm"); // alarm.Title = "Alarm Title"; // Alarm 的 Title 屬性無法修改 alarm.Content = "Alarm Content"; alarm.Sound = new Uri("/Assets/SuperMario.mp3", UriKind.Relative); alarm.BeginTime = DateTime.Now.AddMinutes(1); alarm.ExpirationTime = DateTime.Now.AddDays(1); alarm.RecurrenceType = RecurrenceInterval.Daily; ; // 程序中如果有沒有指定的 Alarm,則 Add,否則 Replace if (ScheduledActionService.Find("alarm") == null) ScheduledActionService.Add(alarm); else ScheduledActionService.Replace(alarm); ShowRegisteredAlarm(); } } }
2、演示 Reminder(按一個時間計划彈出提示信息)
ReminderDemo.xaml
<phone:PhoneApplicationPage x:Class="Demo.BackgroundTask.ReminderDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <StackPanel Orientation="Vertical"> <TextBlock Text="Reminder 的樣式" /> <Image Source="/BackgroundTask/Reminder.png" /> <Button x:Name="btnRegister" Content="注冊一個一分鍾后啟動的 Reminder" Click="btnRegister_Click" /> <TextBlock x:Name="lblMsg" /> <TextBlock x:Name="lblParam" /> </StackPanel> </phone:PhoneApplicationPage>
ReminderDemo.xaml.cs
/* * ScheduledAction - 所有計划活動的基類,抽象類。ScheduledNotification 和 ScheduledTask 繼承自此類 * ScheduledNotification - 用於按時間計划彈出信息,抽象類 * * Reminder - 按一個時間計划彈出提示信息,每一個程序在某個時刻最多只能有 50 個提示信息。Reminder 繼承自 ScheduledNotification * Name - Reminder 的名稱,此名稱即 ID * Title - 提示的標題 * Content - 提示的詳細內容 * BeginTime - 在此時間點彈出提示信息(系統每隔一分鍾會統一調度所有 ScheduledNotification 一次,也就是說系統會在 BeginTime 所指定時間點的一分鍾之內彈出相關信息) * ExpirationTime - 提示的過期時間。當彈出提示信息后,如果用戶選擇了“推遲”,則一段時間過后還會繼續彈出此次計划的提示信息,但是在此值所指定的時間點過后則永遠不再彈出此次計划的信息 * RecurrenceType - 彈出信息的時間計划類型。Microsoft.Phone.Scheduler.RecurrenceInterval 枚舉:None|Daily|Weekly|Monthly|EndOfMonth|Yearly * NavigationUri - 單擊彈出的提示框后,所鏈接到的目標地址(Uri 類型) * IsEnabled - 目前此值無用 * IsScheduled - 此 ScheduledAction 之后是否有執行計划(只讀字段) * * ScheduledActionService - 管理 ScheduledAction 的類 * ScheduledActionService.GetActions<T>() where T : ScheduledAction - 查找系統中已注冊的 ScheduledAction 類型的數據 * ScheduledAction Find(string name) - 按名稱查找指定的 ScheduledAction * ScheduledAction.Remove(string name) - 按名稱刪除指定的 ScheduledAction * ScheduledAction.Add(ScheduledAction action) - 注冊一個新的 ScheduledAction * ScheduledAction.Replace(ScheduledAction action) - 更新指定的 ScheduledAction */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.Scheduler; namespace Demo.BackgroundTask { public partial class ReminderDemo : PhoneApplicationPage { public ReminderDemo() { InitializeComponent(); this.Loaded += new RoutedEventHandler(AlarmDemo_Loaded); } protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { if (NavigationContext.QueryString.Count > 0) { lblParam.Text = "參數 param 的值為:" + this.NavigationContext.QueryString["param"]; lblParam.Text += Environment.NewLine; lblParam.Text += "參數 param2 的值為:" + this.NavigationContext.QueryString["param2"]; } base.OnNavigatedTo(e); } void AlarmDemo_Loaded(object sender, RoutedEventArgs e) { ShowRegisteredReminder(); } // 顯示程序中已有的 Reminder private void ShowRegisteredReminder() { // IEnumerable<ScheduledNotification> notifications = ScheduledActionService.GetActions<ScheduledNotification>(); IEnumerable<Reminder> reminders = ScheduledActionService.GetActions<Reminder>(); lblMsg.Text = "程序中已注冊的 Reminder 的名稱為:" + string.Join(",", reminders.Select(p => p.Name).ToList()); } private void btnRegister_Click(object sender, RoutedEventArgs e) { // 查找程序中指定的 Reminder,如果沒有則實例化一個 Reminder reminder = ScheduledActionService.Find("reminder") as Reminder; if (reminder == null) reminder = new Reminder("reminder"); reminder.Title = "Reminder Title"; reminder.Content = "Reminder Content"; reminder.BeginTime = DateTime.Now.AddMinutes(1); reminder.ExpirationTime = DateTime.Now.AddDays(1); reminder.RecurrenceType = RecurrenceInterval.Daily; ; reminder.NavigationUri = new Uri("/BackgroundTask/ReminderDemo.xaml?param=abc¶m2=xyz", UriKind.Relative); // 程序中如果有沒有指定的 Reminder,則 Add,否則 Replace if (ScheduledActionService.Find("reminder") == null) ScheduledActionService.Add(reminder); else ScheduledActionService.Replace(reminder); ShowRegisteredReminder(); } } }
OK
[源碼下載]