Click here to Skip to main content
Click here to Skip to main content

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

, 21 Jul 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
An application for SmartPhones to create multiple alarms.
Application's Default Screen Application's Menu In Action
Setting New Alarm Alarm 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.

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)

Share

About the Author

Hermit Dave
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
Follow on   Twitter

Comments and Discussions

 
QuestionWhich version of OpenNetCF? Pinmemberchtatou18-Jan-10 7:44 
AnswerRe: Which version of OpenNetCF? PinmemberHermit Dave18-Jan-10 8:21 
GeneralRe: Which version of OpenNetCF? PinmemberPPCFun18-Jan-10 12:57 
GeneralRe: Which version of OpenNetCF? PinmemberShafaqat Ali13-Mar-10 0:23 
GeneralGreat.. This is what i was planning to do for Windows Mobile 5. PinmemberNagaraj Muthuchamy12-Nov-08 2:09 
GeneralRe: Great.. This is what i was planning to do for Windows Mobile 5. PinmemberHermit Dave12-Nov-08 2:29 
Generalneed help PinmembermmMMmm37-Feb-08 14:03 
GeneralLicence PinmemberStrawberryFrog20-Oct-06 5:32 
AnswerRe: Licence PinmemberHermit Dave20-Oct-06 5:53 
GeneralRe: Licence PinmemberStrawberryFrog20-Oct-06 12:12 
GeneralPossible problems PinmemberSire40414-Sep-05 21:09 
GeneralPossible solutions PinmemberSire40414-Sep-05 21:27 
GeneralRe: Possible solutions PinmemberHermit Dave14-Sep-05 21:47 
QuestionHow do i learn WinCE Pinmembervikas amin31-Aug-05 4:11 
AnswerRe: How do i learn WinCE PinmemberHermit Dave1-Sep-05 14:16 
GeneralWhy dont you just use RunAppAtTime Pinmemberthaoula@home19-Jul-05 12:19 
GeneralRe: Why dont you just use RunAppAtTime PinmemberHermit Dave19-Jul-05 12:50 
GeneralRe: Why dont you just use RunAppAtTime PinmemberHermit Dave20-Jul-05 3:33 
GeneralDuh... there is no OpenCFNET PinmemberHans Reyz12-Jul-05 22:45 
GeneralRe: Duh... there is no OpenCFNET PinmemberHermit Dave13-Jul-05 2:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 21 Jul 2005
Article Copyright 2005 by Hermit Dave
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid