How to call the background alarm function of Windows Phone 7.1 step by step

Source: Internet
Author: User

Since wp7.1, Windows Phone has opened some background calls, including music, alarm clocks, and players. I believe that background calls may be made during Windows Phone development, the alarm reminder function is required in our software engineering. Now let us know the details step by step.

Including multiple reminders, storage, and display functions.

1. Introduction

Reminder is an application module in Windows Phone 7 of Microsoft academic search (MAS). It is used to remind users of the beginning of each session in the meeting. You can set the reminder start time, end time, reminder message and ringtone, and delete existing reminder. What I need to do is to provide APIs for creating and deleting reminder, and manage existing reminders. Next I will talk about my methods, difficulties, lessons learned, and solutions in the implementation process.

2. How to store reminderlist in memory

ProgramDuring runtime, you need to store temporary reminderlist information in the memory. Next I will talk about my experience and summary.

2.1. Data Structure

It is very important to select the data structure. I initially planned to use the dictionary and tuple of C # And found that Windows Phone does not support tuple, So I defined a class tuple5 and used it as the type of the value of dictionary. Class tuple5CodeAs follows:

 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 }

 

The type of the dictionary that stores the reminderlist is as follows:

Dictionary <Uint, Tuple5 <datetime, datetime,String, Recurrenceinterval, Uri>

 

Uint is a universally unique identifier (UUID) assigned to each reminder. The five parameters in tuple5 are the start time, end time, and message of the reminder, repeat the ring frequency and the address of the audio file. In this way, I can use this dictionary to store our reminderlist.

2.2. Use Windows Phone's alarm

To enable reminder to ring at the set time, we use the new feature Windows Phone SDK 7.1-alarm. I first referred to the official msdn code example. The address is as follows:

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

Then write the code by yourself. The following describes how to set up an alarm.

First, we need to use the namespace of scheduler of Windows Phone. The Code is as follows:

 
UsingMicrosoft. Phone. scheduler;

Then, create an alram object and add it to schedence by using the input start time begintime, End Time stoptime, message, repeat ring frequency recurrence, and sound file address sound. The Code is as follows:

1Alarm alarm =NewAlarm (name );
2Alarm. content = message;
3Alarm. Sound = sound;
4Alarm. begintime = begintime;
5Alarm. expirationtime = stoptime;
6Alarm. recurrencetype = recurrence;
7Scheduledactionservice. Add (Alarm );

 

3. How to store the reminderlist in the file system

Because the user-configured alarm requires deletion and modification when starting the program next time, the existing alarms must be stored in the file system. Next I will talk about my experience.

3.1. serialization failed

At the beginning, I tried to use the xmlserializer and datacontractserializer of C # to store my dictionary. However, after a day's attempt, I finally ended with a failure. Next I will talk about how to implement xmlserializer.

At first, I tried to use xmlserializer to save my dictionary as an XML file, and then read the dictionary directly from the XML file next time. The Code is as follows:

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 }

At the end of the compilation, the system prompts that the URI cannot be serialized. So I changed URI to string, and he still prompted me That the serialization of tuple5 failed. So I studied this carefully and tried several methods, I added the [serializable ()] attribute to the definition of my class tuple5, but still failed. So I began to consider converting dictionary into a file.

3.2. Convert dictionary into a file by yourself

Because I failed to use serialization, I began to save dictionary to a file. The simplest method I used was to store a line of each item in string format. The Code is as follows:

 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 }

 

One thing to note is that Windows Phone uses an independent storage file isolatedstoragefile, which is different from that on a PC.

3.3. Read reminderlist from the file

At each startup, you must be able to read the reminderlist from the file. I wrote a static constructor to read the reminderlist from the file. A static constructor calls an object declared only once. The code for reading reminderlist from a file is as follows:

 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. Other Details

Although the reminder module has been implemented, I have considered many details that need to be improved. The following are two examples.

4.1. Duplicate reminder

When setting a reminder for the same session, the user may modify the settings, but in my program, two alarms will be set, therefore, the caller must delete the alarm (with the ID returned by the previous create alarm) for the same session before creating a new one.

4.2. Delete expired reminder

Some alarms have passed the stoptime, and the system needs to delete these alarms regularly. I clean up expired alarms every time I create a new alarm.

The following is my summary after nearly three days of grinding,If it feels useful to you, please recommend it to us. We believe this is an encouragement for new users who are developing Windows Phone!

Thank you for your criticism!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.