与众不同 windows phone (11) - Background Task(后台任务)之警报(Alarm)和提醒(Reminder)

简介: 原文:与众不同 windows phone (11) - Background Task(后台任务)之警报(Alarm)和提醒(Reminder)[索引页][源码下载] 与众不同 windows phone (11) - Background Task(后台任务)之警报(Alarm)和提醒(Reminder) 作者:webabcd介绍与众不同 windows phone 7.
原文: 与众不同 windows phone (11) - Background Task(后台任务)之警报(Alarm)和提醒(Reminder)

[索引页]
[源码下载]


与众不同 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&param2=xyz", UriKind.Relative);

            // 程序中如果有没有指定的 Reminder,则 Add,否则 Replace
            if (ScheduledActionService.Find("reminder") == null)
                ScheduledActionService.Add(reminder);
            else
                ScheduledActionService.Replace(reminder);

            ShowRegisteredReminder();
        }
    }
}



OK
[源码下载]

目录
相关文章
|
9月前
|
监控 JavaScript Windows
网页未读邮件windows弹窗提醒
因为工作需要经常查看邮件,现写一个js代码用于固定时间刷新邮箱界面并监控新增的未读邮件,再弹出Windows弹框提醒
56 0
|
5月前
|
Shell 数据安全/隐私保护 Windows
Windows Server【开机启动和任务计划程序】实现服务器重启后项目自启动(Windows Server 任务计划程序无法执行问题处理)
Windows Server【开机启动和任务计划程序】实现服务器重启后项目自启动(Windows Server 任务计划程序无法执行问题处理)
196 0
|
12月前
|
安全 Java Windows
​❤️​Windows下创建【任务计划程序】​❤️定期重启服务器中的【JAR包脚本】​❤️实现JAR包重启防止卡顿服务超时中断❤️
​❤️​Windows下创建【任务计划程序】​❤️定期重启服务器中的【JAR包脚本】​❤️实现JAR包重启防止卡顿服务超时中断❤️
269 0
|
存储 数据安全/隐私保护 Windows
小技巧 - 同步苹果手机和 Windows 的提醒事项
小技巧 - 同步苹果手机和 Windows 的提醒事项
676 0
小技巧 - 同步苹果手机和 Windows 的提醒事项
|
Windows
Win系统 - 苹果系统里的“提醒事项”可否同步到 Windows 系统中?
Win系统 - 苹果系统里的“提醒事项”可否同步到 Windows 系统中?
91 0
Win系统 - 苹果系统里的“提醒事项”可否同步到 Windows 系统中?
|
Android开发 iOS开发 Windows
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
不久前,随着最后一家WP手机厂商惠普宣布取消今后Windows Phone的研发计划,以及微软官方声明对WP8.1系统今后所有升级维护的终止,WP手机,作为曾经和安卓手机、苹果手机并驾齐驱的三大智能手机之一,正式寿终正寝。
1277 0
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
|
XML 开发框架 前端开发
Windows Phone快速入门需掌握哪些能力
在此之前,先普及下Windows Phone的概念和开发工具的介绍。 Windows Phone是微软公司开发的手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验集成至手机中。2012年6月21日,微软正式发布Windows Phone 8,采用和Windows 8相同的Windows NT内核,同时也针对市场的Windows Phone 7.5发布Windows Phone 7.8。
135 0
Windows Phone快速入门需掌握哪些能力
|
编解码 前端开发 JavaScript
Windows Phone 下开发 LBS 应用
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
164 0
|
移动开发 Android开发 开发者
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
在Build 2014大会上,微软正式发布了传闻已久的Windows Phone 8.1系统,所有的Windows Phone 8手机都可以升级,微软这次可谓是十分厚道。虽然并非迭代升级,但WP 8.1还是拥有很多重大更新,对于微软进一步完善移动平台拥有积极的意义。下面,就一起来了解一下WP 8.1的主要新特性。
234 0
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
|
API C++ Windows
windows C++ 多任务并发执行设计
windows C++ 多任务并发执行设计
214 0
windows C++ 多任务并发执行设计