65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 OpenNetCF 和 C# 为 SmartPhone(Windows Mobile 2003)创建智能闹钟

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.58/5 (7投票s)

2005年7月12日

CPOL

3分钟阅读

viewsIcon

123147

downloadIcon

1256

用于智能手机创建多个闹钟的应用程序。

Application's Default Screen Application's Menu In Action
Setting New Alarm Alarm in Action

引言

今年生日时,我给自己买了一部Sagem myS-7智能手机。在使用过很多其他手机后,我对智能手机中的闹钟选项有点失望。它非常基本。您只能选择时间。(在“设置”/“更多”/“日期和时间”下。“贪睡”选项会贪睡 10 分钟,您无法更改。所以我开始编写一个闹钟,它能完成我大部分想要的功能。)

我从Timer对象开始,它会在正确的时间触发事件。我遇到的唯一问题是它无法将手机从待机模式中唤醒。我在网上搜索了一下,我意识到我应该设置系统通知,才能使手机从待机模式中唤醒。我研究了一下执行本机代码,但找到了OpenNetCF。它有一组好的类,并且也具有通知功能。

使用代码

代码非常简单。设置一个Timer对象,每小时检查一次即将到来的闹钟事件。如果在一小时内找到闹钟,它会设置另一个Timer对象来执行并从OpenNETCF.Win32.Notify设置Notify.RunAppAtTime。实际播放闹钟(公鸡叫声)的应用程序是一个独立的智能手机应用程序,它使用SoundPlayer控件从OpenNETCF.Windows.Forms播放Wav文件。

为了持久化闹钟数据,我使用了DataSetReadXmlWriteXml

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();
}

关注点

默认情况下,当您将数据保存为XML时,该文件会创建在根目录下。要获取应用程序的路径,您需要使用Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName。这将返回* /Storage Card/Program Files/SmartAlarm.exe*。使用Assembly.GetExecutingAssembly().GetModules()[0].Name获取程序集名称,并将其替换为"",您就有了路径。

我注意到以下几点,这可能有助于创建智能手机应用程序

  • Build Cab 选项失败。但它确实在*Obj/Release*文件夹下创建了一个*Build.bat*文件(如果您正在进行发布构建包)。
  • 更新批处理文件,用于 Cab 生成,使用*C:\Program Files\Windows CE Tools\wce420\SMARTPHONE 2003\Tools\CabwizSP.exe*,而不是*C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\bin\cabwiz.exe*。
  • 从*C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\wce400\armv4*复制*vsd_setup.dll*到*C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\Smartphone\wce400\armv4*。
  • 从*C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\wce400\x86*复制*vsd_setup.dll*到*C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\Smartphone\wce400\x86*。
  • 对于OpenNetCF,手动在手机中安装*OpenNETCF.SDF.smp.armv4.cab*。
  • 如果您正在使用OpenNetCF,模拟器默认情况下将不再工作。如果您想使用模拟器,请将OpenNetCF引用的程序集添加为项目中的内容。
  • 最后,当DateTime控件接收到控制时,它不允许任何其他元素轻易地获得焦点。这就是为什么我将它的接收焦点设置在最后。这是控件的限制。
© . All rights reserved.