Click here to Skip to main content
15,881,757 members
Articles / Mobile Apps / Windows Mobile

Smart Alarm Using OpenNetCF and C# for SmartPhone (Windows Mobile 2003)

Rate me:
Please Sign up or sign in to vote.
4.58/5 (7 votes)
21 Jul 2005CPOL3 min read 121.2K   1.2K   55   21
An application for SmartPhones to create multiple alarms.
Application's Default ScreenApplication's Menu In Action
Setting New AlarmAlarm in Action

Introduction

I got myself a SmartPhone Sagem myS-7 for my b'day this year. Having used a lot of other phones I was a bit disappointed by the Alarm option within SmartPhone. It's very very basic. You can only select the time. (Under Settings / More / Date and Time. The Snooze option sleeps for 10 minutes and you don't have an option to change that. So I got myself to writing an alarm which did most of the things I wanted.)

I started with the Timer object and it was firing the event at the right time. The only problem I was having was that it just wouldn't take the phone out of Standby mode. I Googled around a bit and I realized that I was supposed to set system notification in order for the phone to come out of standby. I looked around to executing native code but I found OpenNetCF. It's got a good set of classes and it had the notification functionality as well.

Using the code

The code is pretty straightforward. Set a Timer object to do an hourly check for upcoming Alarm events. If an alarm is found within the next hour it sets another Timer object to execute and set a Notify.RunAppAtTime from OpenNETCF.Win32.Notify. The application actually playing the alarm (rooster at its best) is a standalone SmartPhone app and plays the Wav file using the SoundPlayer control from OpenNETCF.Windows.Forms.

For persisting alarm data, I used DataSet's ReadXml and WriteXml.

C#
private void GetNextNotificationInfo()
{
    this.nextAlarmUp = 0;
    DataSet dsAlarms = new DataSet();
    if(File.Exists(ConfigFile) == true)
    {
        dsAlarms.ReadXml(ConfigFile);
        if(dsAlarms.Tables.Count != 0 && dsAlarms.Tables[0].Rows.Count != 0)
        {
            DateTime dtCurrent = DateTime.Now;
            long CurrentTimeInTicks = dtCurrent.TimeOfDay.Ticks;
            int currentDayOfWeek = (int)dtCurrent.DayOfWeek;
            string filterExpression = "Enabled = true AND TimeInTicks >= " 
                + CurrentTimeInTicks + " AND Days LIKE '%" + 
                Convert.ToString(currentDayOfWeek) + "%'";
            string sortExpression = "TimeInTicks DESC";
            DataRow[] drActiveAlarms = 
              dsAlarms.Tables[0].Select(filterExpression, 
              sortExpression);

            if(drActiveAlarms.Length > 0)
            {
                DataRow drSelectedAlarm = drActiveAlarms[0];
                long SelectedTimeInTicks = 
                  Convert.ToInt64(drSelectedAlarm["TimeInTicks"]);

                if(SelectedTimeInTicks > CurrentTimeInTicks)
                {
                    TimeSpan tsSelectedTime = new TimeSpan(SelectedTimeInTicks);
                    TimeSpan tsCurrentTime = dtCurrent.TimeOfDay;

                    TimeSpan tsDifference = tsSelectedTime - tsCurrentTime;

                    if(tsDifference.TotalSeconds < 3600)
                    {
                        // set timerNotify to fire 1 minute before alarm time
                        // and set the AlarmExec to run after a minute

                        this.nextAlarmUp = Convert.ToInt32(drSelectedAlarm["ID"]);
                        this.timerNotify.Interval = 
                         Convert.ToInt32(tsDifference.TotalMilliseconds - 60*1000);
                        this.timerNotify.Enabled = true;
                    }
                    else
                    {
                        this.nextAlarmUp = 0;
                        this.timerNotify.Enabled = false;
                    }
                }
            }
        }
    }
}

private void timerNotify_Tick(object sender, System.EventArgs e)
{
    this.timerNotify.Enabled = false;
    Notify.RunAppAtTime(this.AppPath + "SDAlarmExec.exe", 
                             DateTime.Now.AddMinutes(1));
    this.ExecuteDisable();
    GetNextNotificationInfo();
}

Points of Interest

By default when you save data as XML, the file is created in the root directory. To get the application's path, you need to use Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName. This returns /Storage Card/Program Files/SmartAlarm.exe. Fetch the assembly name using Assembly.GetExecutingAssembly().GetModules()[0].Name and replace it with "" and you have the path.

I noticed the following points which might help when creating SmartPhone apps:

  • The Build Cab option fails. But it does create a Build.bat file under the Obj/Release folder (if you are doing a release build package).
  • Update the batch file for Cab generation to use C:\Program Files\Windows CE Tools\wce420\SMARTPHONE 2003\Tools\CabwizSP.exe instead of C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\bin\cabwiz.exe.
  • Copy vsd_setup.dll from C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\wce400\armv4 to C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\Smartphone\wce400\armv4.
  • Copy vsd_setup.dll from C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\wce400\x86 to C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\Smartphone\wce400\x86.
  • For OpenNetCF, manually install OpenNETCF.SDF.smp.armv4.cab in the phone.
  • If you are using OpenNetCF, the emulator doesn't work any more by default. If you would like to use the emulator add the OpenNetCF referenced assemblies as content in the Project.
  • Lastly when the DateTime control receives control, it doesn't allow any other elements to have focus that easily. That is the reason why I have set its receive focus at the very end. It's a limitation of the control.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Self
United Kingdom United Kingdom
A father, a husband and geek. I am opinionated around Microsoft technology stack as I have worked on it for most of my professional life. I also organise @WinRTDev - Windows 8 developer group in London.

For the last 2 years, I have been doing hobbyist development for Windows Phone and Windows 8. In that time I have developed a few apps - using publisher name : Invoke IT Limited
Windows Phone: Cineworld, Alarm Clock Free, Slydr (each has 35K downloads and growing) plus another 20 odd apps.
Windows 8: Cineworld (20K), Nature Sounds Free and a few more

After 13 years of professional enterprise development, I now work in a fast growing Mobile Banking / Commerce company.

I am a mobile / tablet enthusiast and you will find me tweeting / blogging about those

Comments and Discussions

 
QuestionWhich version of OpenNetCF? Pin
PPCFun18-Jan-10 6:44
PPCFun18-Jan-10 6:44 
AnswerRe: Which version of OpenNetCF? Pin
Hermit Dave18-Jan-10 7:21
professionalHermit Dave18-Jan-10 7:21 
GeneralRe: Which version of OpenNetCF? Pin
PPCFun18-Jan-10 11:57
PPCFun18-Jan-10 11:57 
GeneralRe: Which version of OpenNetCF? Pin
Shafaqat Ali12-Mar-10 23:23
Shafaqat Ali12-Mar-10 23:23 
GeneralGreat.. This is what i was planning to do for Windows Mobile 5. Pin
Nagaraj Muthuchamy12-Nov-08 1:09
professionalNagaraj Muthuchamy12-Nov-08 1:09 
GeneralRe: Great.. This is what i was planning to do for Windows Mobile 5. Pin
Hermit Dave12-Nov-08 1:29
professionalHermit Dave12-Nov-08 1:29 
Generalneed help Pin
mmMMmm37-Feb-08 13:03
mmMMmm37-Feb-08 13:03 
GeneralLicence Pin
StrawberryFrog20-Oct-06 4:32
StrawberryFrog20-Oct-06 4:32 
AnswerRe: Licence Pin
Hermit Dave20-Oct-06 4:53
professionalHermit Dave20-Oct-06 4:53 
GeneralRe: Licence Pin
StrawberryFrog20-Oct-06 11:12
StrawberryFrog20-Oct-06 11:12 
GeneralPossible problems Pin
Sire40414-Sep-05 20:09
Sire40414-Sep-05 20:09 
GeneralPossible solutions Pin
Sire40414-Sep-05 20:27
Sire40414-Sep-05 20:27 
GeneralRe: Possible solutions Pin
Hermit Dave14-Sep-05 20:47
professionalHermit Dave14-Sep-05 20:47 
QuestionHow do i learn WinCE Pin
vikas amin31-Aug-05 3:11
vikas amin31-Aug-05 3:11 
AnswerRe: How do i learn WinCE Pin
Hermit Dave1-Sep-05 13:16
professionalHermit Dave1-Sep-05 13:16 
GeneralWhy dont you just use RunAppAtTime Pin
thaoula@home19-Jul-05 11:19
thaoula@home19-Jul-05 11:19 
GeneralRe: Why dont you just use RunAppAtTime Pin
Hermit Dave19-Jul-05 11:50
professionalHermit Dave19-Jul-05 11:50 
GeneralRe: Why dont you just use RunAppAtTime Pin
Hermit Dave20-Jul-05 2:33
professionalHermit Dave20-Jul-05 2:33 
GeneralDuh... there is no OpenCFNET Pin
Hans Reyz12-Jul-05 21:45
Hans Reyz12-Jul-05 21:45 
GeneralRe: Duh... there is no OpenCFNET Pin
Hermit Dave13-Jul-05 1:15
professionalHermit Dave13-Jul-05 1:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.