Click here to Skip to main content
11,707,470 members (71,449 online)
Click here to Skip to main content

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

, 21 Jul 2005 CPOL 99.9K 1K 55
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

You may also be interested in...

Comments and Discussions

 
QuestionWhich version of OpenNetCF? Pin
chtatou18-Jan-10 6:44
memberchtatou18-Jan-10 6:44 
AnswerRe: Which version of OpenNetCF? Pin
Hermit Dave18-Jan-10 7:21
memberHermit Dave18-Jan-10 7:21 
GeneralRe: Which version of OpenNetCF? Pin
PPCFun18-Jan-10 11:57
memberPPCFun18-Jan-10 11:57 
GeneralRe: Which version of OpenNetCF? Pin
Shafaqat Ali12-Mar-10 23:23
memberShafaqat 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
memberNagaraj 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
memberHermit Dave12-Nov-08 1:29 
Generalneed help Pin
mmMMmm37-Feb-08 13:03
membermmMMmm37-Feb-08 13:03 
GeneralLicence Pin
StrawberryFrog20-Oct-06 4:32
memberStrawberryFrog20-Oct-06 4:32 
AnswerRe: Licence Pin
Hermit Dave20-Oct-06 4:53
memberHermit Dave20-Oct-06 4:53 
GeneralRe: Licence Pin
StrawberryFrog20-Oct-06 11:12
memberStrawberryFrog20-Oct-06 11:12 
GeneralPossible problems Pin
Sire40414-Sep-05 20:09
memberSire40414-Sep-05 20:09 
GeneralPossible solutions Pin
Sire40414-Sep-05 20:27
memberSire40414-Sep-05 20:27 
GeneralRe: Possible solutions Pin
Hermit Dave14-Sep-05 20:47
memberHermit Dave14-Sep-05 20:47 
QuestionHow do i learn WinCE Pin
vikas amin31-Aug-05 3:11
membervikas amin31-Aug-05 3:11 
AnswerRe: How do i learn WinCE Pin
Hermit Dave1-Sep-05 13:16
memberHermit Dave1-Sep-05 13:16 
GeneralWhy dont you just use RunAppAtTime Pin
thaoula@home19-Jul-05 11:19
memberthaoula@home19-Jul-05 11:19 
GeneralRe: Why dont you just use RunAppAtTime Pin
Hermit Dave19-Jul-05 11:50
memberHermit Dave19-Jul-05 11:50 
GeneralRe: Why dont you just use RunAppAtTime Pin
Hermit Dave20-Jul-05 2:33
memberHermit Dave20-Jul-05 2:33 
GeneralDuh... there is no OpenCFNET Pin
Hans Reyz12-Jul-05 21:45
memberHans Reyz12-Jul-05 21:45 
GeneralRe: Duh... there is no OpenCFNET Pin
Hermit Dave13-Jul-05 1:15
memberHermit Dave13-Jul-05 1: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
Web03 | 2.8.150819.1 | Last Updated 21 Jul 2005
Article Copyright 2005 by Hermit Dave
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid