`
tomhibolu
  • 浏览: 1430638 次
文章分类
社区版块
存档分类
最新评论

如何调用windows phone 7.1的后台闹钟功能 step by step

 
阅读更多

自从 WP7.1之后,windows phone 开放一些后台调用,包括音乐,闹钟,播放器等,相信在做windows phone开发时,有可能会调用后台, 恰好我们的软件工程中需要用到闹钟提醒功能,现在就把具体的细节一步一步告诉大家。

包括实现多项提醒,存储和显示等功能。

1. 简介

Reminder是我们Microsoft Academic Search (MAS) 的Windows Phone 7的一个应用的模块,用来提醒用户会议中每个session的开始。用户可以设置reminder的开始时间,结束时间,reminder的消息以及铃声,同时用户也可以删除已经存在的reminder。我需要做的就是提供创建和删除reminder的API,已经管理现有的reminders。下面我就谈一谈我在实现过程中的方法,遇到的困难,教训以及解决方案。

2. 如何在Memory中存储ReminderList

程序运行时,需要在内存中存储临时的ReminderList的信息。下面我谈谈这个部分我的经验与总结。

2.1. 数据结构

选择何时的数据结构是非常重要的。我一开始打算使用C#的Dictionary和Tuple,结果发现Windows Phone并不支持Tuple,于是我自己定义了一个class Tuple5并且把它用作Dictionary的Value的Type。Class Tuple5的代码如下:

 1 public class Tuple5<T1, T2, T3, T4, T5>
 2 {
 3     public T1 Item1 { get; set; }
 4     public T2 Item2 { get; set; }
 5     public T3 Item3 { get; set; }
 6     public T4 Item4 { get; set; }
 7     public T5 Item5 { get; set; }
 8 
 9     public Tuple5(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
10     {
11         Item1 = item1;
12         Item2 = item2;
13         Item3 = item3;
14         Item4 = item4;
15         Item5 = item5;
16     }
17 }

 

存储ReminderList的Dictionary的type如下:

Dictionary<uint, Tuple5<DateTime, DateTime, string, RecurrenceInterval, Uri>>

 

其中uint是我给每个Reminder分配的一个Universally Unique Identifier (UUID),Tuple5里面五个参数分别为Reminder的开始时间,结束时间,消息,重复响铃频率和声音文件的地址。这样我就可以用这个Dictionary来存储我们的ReminderList了。

2.2. 使用Windows Phone的Alarm

为了让我们的Reminder能在设定的时间响铃,我们使用了Windows Phone SDK 7.1的新功能——Alarm。我首先参考了MSDN官方的代码示例,地址如下:

http://msdn.microsoft.com/en-us/library/hh202965(v=vs.92).aspx

然后自己开始编写代码。下面我主要讲一下如何设置一个Alarm。

首先我们需要使用Windows Phone的scheduler的namespace,代码如下:

using Microsoft.Phone.Scheduler;

然后就是使用传进来的开始时间beginTime,结束时间stopTime,消息message,重复响铃频率recurrence和声音文件的地址sound来创建一个Alram对象并且加入Scheduler,代码如下:

1 Alarm alarm = new Alarm(name);
2 alarm.Content = message;
3 alarm.Sound = sound;
4 alarm.BeginTime = beginTime;
5 alarm.ExpirationTime = stopTime;
6 alarm.RecurrenceType = recurrence;
7 ScheduledActionService.Add(alarm);

 

3. 如何在文件系统中存储ReminderList

因为用户设置的Alarm在下次启动程序的时候还要求能够删除和修改,所以必须要将现有的Alarms存在文件系统里。下面我讲一下我的经验。

3.1. Serialization的失败

我一开始尝试使用C#的XmlSerializer和DataContractSerializer来存储我的Dictionary,但是我经过一天的尝试,最终以失败结束。下面我就XmlSerializer谈谈我的实现方法。

我一开始尝试使用XmlSerializer来将我的Dictionary存成xml文件,然后下次直接从xml文件读取这个Dictionary。代码如下:

 1 XmlSerializer ser = new XmlSerializer(typeof(Dictionary<uint, Tuple5<DateTime, DateTime, string, RecurrenceInterval, Uri>>));
 2 
 3 // write
 4 using (var stream = File.Create("ReminderList.xml"))
 5 {
 6     ser.Serialize(stream, ReminderList); // your instance
 7 }
 8 
 9 // read
10 using (var stream = File.OpenRead("ReminderList.xml"))
11 {
12     ReminderList = (Dictionary<uint, Tuple5<DateTime, DateTime, string, RecurrenceInterval, Uri>>)ser.Deserialize(stream);
13 }


最后编译时提示我Uri不能被Serialized。于是我将Uri改成string,他还是提示我Tuple5的Serialization出错,于是我仔细研究这个,尝试了几种方法,包括在我的class Tuple5的定义加上[Serializable()]的属性,最后还是没有成功,于是我开始考虑自己将Dictionary转化成文件。

3.2. 自己将Dictionary转化成文件

因为使用Serialization失败,我开始自己将Dictionary存入文件,我使用了最简单的方法,就是将每个item一行行以string形式存入文件,代码如下:

 1 // Obtain the virtual store for the application.
 2 IsolatedStorageFile myStore = IO.GetUserStore();
 3 myStore.CreateDirectory("Reminder");
 4 
 5 // Specify the file path and options.
 6 using (var isoFileStream = new IsolatedStorageFileStream("Reminder\\ReminderList.dat", FileMode.OpenOrCreate, myStore))
 7 {
 8     //Write the data
 9     using (var isoFileWriter = new StreamWriter(isoFileStream))
10     {
11         foreach (KeyValuePair<uint, Tuple5<DateTime, DateTime, string, RecurrenceInterval, Uri>> kvp in ReminderList)
12         {
13             isoFileWriter.WriteLine(Convert.ToString(kvp.Key));
14             isoFileWriter.WriteLine(kvp.Value.Item1.ToString());
15             isoFileWriter.WriteLine(kvp.Value.Item2.ToString());
16             isoFileWriter.WriteLine(kvp.Value.Item3);
17             isoFileWriter.WriteLine(kvp.Value.Item4.ToString());
18             if (kvp.Value.Item5 != null)
19             {
20                 isoFileWriter.WriteLine(kvp.Value.Item5.AbsoluteUri);
21             }
22             else
23             {
24                 isoFileWriter.WriteLine("");
25             }
26         }
27     }
28 }

 

有一点要说明的是,Windows Phone使用的是独立的存储文件IsolatedStorageFile,与PC上并不一样。

3.3. 从文件中读取ReminderList

每次启动时,都要能够从文件中读取ReminderList。我是写了一个静态构造函数来从文件读取ReminderList。静态构造函数有一个对象被声明就会调用,且整个程序只调用一次,从文件读取ReminderList的代码如下:

 1 // Obtain a virtual store for the application.
 2 IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
 3 
 4 try
 5 {
 6     // Specify the file path and options.
 7     using (var isoFileStream = new IsolatedStorageFileStream("Reminder\\ReminderList.dat", FileMode.Open, myStore))
 8     {
 9         // Read the data.
10         using (var isoFileReader = new StreamReader(isoFileStream))
11         {
12             string line = null;
13             while ((line = isoFileReader.ReadLine()) != null)
14             {
15                 uint newId = Convert.ToUInt32(line);
16                 line = isoFileReader.ReadLine();
17                 DateTime startTime = DateTime.Parse(line);
18                 line = isoFileReader.ReadLine();
19                 DateTime stopTime = DateTime.Parse(line);
20                 line = isoFileReader.ReadLine();
21                 string message = line;
22                 line = isoFileReader.ReadLine();
23                 RecurrenceInterval recurrence = RecurrenceInterval.None;
24                 switch (line)
25                 {
26                 case "None": recurrence = RecurrenceInterval.None; break;
27                 case "Daily": recurrence = RecurrenceInterval.Daily; break;
28                 case "EndOfMonth": recurrence = RecurrenceInterval.EndOfMonth; break;
29                 case "Monthly": recurrence = RecurrenceInterval.Monthly; break;
30                 case "Weekly": recurrence = RecurrenceInterval.Weekly; break;
31                 case "Yearly": recurrence = RecurrenceInterval.Yearly; break;
32                 default: recurrence = RecurrenceInterval.None; break;
33                 }
34 
35                 line = isoFileReader.ReadLine();
36                 Uri sound = null;
37                 if (line != "")
38                 {
39                     sound = new Uri(line, UriKind.Relative);
40                 }
41                 Tuple5<DateTime, DateTime, string, RecurrenceInterval, Uri> newReminder = new Tuple5<DateTime, DateTime, string, RecurrenceInterval, Uri>(startTime, stopTime, message, recurrence, sound);
42                 ReminderList.Add(newId, newReminder);
43             }
44         }
45     }
46 }
47 catch
48 {
49     // TODO:
50 }

 

4. 其他细节

虽然实现了Reminder的模块,我还是考虑了很多需要完善的细节,下面举出两个例子。

4.1. 重复的Reminder

用户在设置同一个session的reminder的时候,可能会修改设置,但是在我的程序里就会导致设置两个alarms,于是调用者对于同一个session必须先delete这个alarm(用之前create alarm返回的ID),然后才能创建新的。

4.2. 删去过期的Reminder

有些Alarms已经过了stopTime了,系统需要定期删掉这些Alarms。我是在每次创建新的alarm之前清理过期的alarms。

下面就是我花了将近三天时间研磨总结出来的,如果感觉对你有用,请推荐我们,相信这是对我们新手开发windows phone过程中的鼓励!

0
1
分享到:
评论

相关推荐

    Windows Phone 7.1 SDK 在线安装包

    Windows Phone 7.1 SDK 在线安装包

    Windows Phone 7.1 记账软件

    Windows Phone 7.1 记账软件是一款专为Windows Phone 7.1操作系统设计的财务管理应用,其核心功能是帮助用户轻松记录日常生活中的收支情况。该软件不仅在手机端提供了便捷的记账服务,还包含了PC端同步工具,确保...

    Windows Phone SDK7.1安装图解

    ### Windows Phone SDK 7.1 安装指南与解析 #### 一、Windows Phone SDK 7.1 简介 Windows Phone SDK 7.1是微软为开发者提供的软件开发工具包,旨在支持Windows Phone 7系列手机上的应用程序开发。这款SDK包含了...

    Windows Phone SDK v7.1.1 开发工具.exe

    Windows Phone SDK 7.1.1 更新在现有的 Windows Phone SDK 7.1 的基础上提供了更多功能。使用此更新,您可以更加轻松地开发可在 256 MB 设备上运行的优化应用程序和游戏。此更新包括新的 Windows Phone 仿真程序 256...

    WindowsPhone SDK7.1 手机开发

    本资源是进行移动开发时候必须安装的。不过是在线安装,在您安装了VS2010之后,先安装SP1补丁,然后再进行此资源的安装。

    Windows Phone 7 SDK 7.1 类声明源码

    Windows Phone 7 SDK 7.1 是微软为开发者提供的一个工具包,用于构建针对Windows Phone 7设备的应用程序。这个SDK包含了大量的类库、开发工具和文档,使得开发者能够使用C#、VB.NET或XAML等语言进行编程。在描述中...

    Windows Phone Software Development Kit (SDK) 7.1

    总的来说,Windows Phone SDK 7.1是开发人员构建创新Windows Phone应用程序的关键工具,它提供了全面的开发环境、框架、测试工具和社区支持,使开发者能够充分利用Windows Phone平台的功能和潜力。通过深入理解和...

    Windows Phone 7 SDK 7.1 开发帮助文档

    Windows Phone 7 SDK 7.1 开发帮助文档是一份详尽的资源,旨在协助开发者在Windows Phone平台上创建高效、功能丰富的应用程序。该文档由Sandcastle工具生成,它是一个专门用于构建API文档的开源项目,能够将.NET ...

    windowsphone SDK 精简版

    Windows Phone SDK 7.1 精简版是一款专为开发者设计的工具集,用于创建、测试和调试针对Windows Phone 7.1操作系统(也称为Mango更新)的应用程序。这款SDK提供了一系列必要的组件,帮助开发者在没有完整版本的开发...

    Windows Phone官方开发实例

    《Windows Phone官方开发实例》是Nokia为开发者提供的宝贵资源,涵盖了Windows Phone 7.1平台上的多个关键功能和技术。这个实例集旨在帮助开发者深入理解并掌握如何在Windows Phone平台上构建高效、用户友好的应用...

    Windows Phone 开发所需补丁

    本文将详细讲解在Windows Phone 7.1(也称为Mango更新)环境下,使用Visual Studio 2010进行开发时所需的关键知识点。 首先,Windows Phone 7.1 Mango更新是微软对Windows Phone操作系统的一次重大升级,它带来了...

    Window Phone 7 SDK7.1开发帮助文档

    Window Phone 7 SDK7.1开发帮助文档

    仿Windows Phone博客导航源码

    仿Windows Phone博客导航源码 程序介绍: Windows Phone Blog Menu是一个Silverlight导航控件,看起来像Windows Phone 7。 控件中的tiles链接到web站点在你的博客中使用这个控件可以表达你对WP7的喜爱。 Windows...

    Windows Phone 8 API一览

    .NET API 是Windows Phone 8 支持的主要托管代码库,它继承了Windows Phone OS 7.1的System和Microsoft.Phone命名空间,同时增加了新的特性和功能。例如,Microsoft.Phone.Wallet用于处理移动支付和钱包服务,...

    Windows Phone 7开发环境安装教程汇总

    在进入Windows Phone 7...通过以上步骤,你将成功搭建一个功能完备的Windows Phone 7开发环境,可以开始编写你的第一个应用了。记住,不断实践和学习是提升开发能力的关键。祝你在Windows Phone开发之旅中一切顺利!

    iphone4 3.1 7.1shsh

    iphone4 3.1 7.1SHSH

    windows phone power tools offline installer

    Windows Phone Power Tools(WPPT)是一款专为Windows Phone开发者设计的强大工具集,它提供了丰富的功能,帮助开发者更高效地管理和调试Windows Phone应用。离线安装器(offline installer)则允许用户在没有网络...

    Windows Phone USB_v4.8.2345_驱动

    总的来说,Windows Phone USB驱动是连接和管理Windows Phone设备不可或缺的一部分,它确保了设备与电脑之间的顺畅通信,提供了多种实用功能,同时也需要用户定期检查更新以保持最佳性能和兼容性。

    Step into Windows Phone 8 进入Windows Phone8开发殿堂

    Windows Phone 8是微软公司针对移动设备推出的智能手机操作系统,与Windows 8操作系统共享内核,并具有与Windows 8相似的用户界面和体验。Windows Phone 8的推出是微软在智能手机市场与苹果iOS和谷歌Android竞争的...

    7.1SHSH下载iPhone3.1

    相信大家已经了解到 ios7 1 版本将是稳定性 功能性都大幅度升级的一版固件 ios7 0 3 刚刚发布了丌到一周 丌少果粉已经开始关心 ios7 1 正式版什么时候发布

Global site tag (gtag.js) - Google Analytics