[MiluGPS]时间同步功能

  因为手机内置电池的老化,在手机更换电池时,经常造成时间恢复到系统的默认时间,有时可能因为附近没有其它方式获和较准确的时间而造成不便。微软的ActiveSync就有同步时间的功能,只要插上USB线连接上ActiveSync,就会自动更新手机上的时间。
  因为GPS可以获得非常精确的时间,所以也可以利用GPS的功能,进行同步时间。获得GPS的时间很容易,主要是设置系统的时间需要调用一些API。

 1//Win32.cs
 2//定义SetSystemTime和SYSTEMTIME结构
 3using System;
 4using System.Collections.Generic;
 5using System.Text;
 6using System.Runtime.InteropServices;
 7
 8namespace MiluGPS {
 9    class Win32 {
10        public struct SYSTEMTIME {
11            public ushort wYear;
12            public ushort wMonth; 
13            public ushort wDayOfWeek; 
14            public ushort wDay; 
15            public ushort wHour; 
16            public ushort wMinute; 
17            public ushort wSecond; 
18            public ushort wMilliseconds; 
19        }

20        [DllImport("coredll.dll")]
21        public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
22
23    }

24}

25

 

 1        //同步时间
 2        private void menuItem5_Click(object sender, EventArgs e) {
 3            ///
 4            // Set the clock ahead one hour
 5            Win32.SYSTEMTIME st = new Win32.SYSTEMTIME();
 6            //SystemTime时间格式与UTC一样。
 7              //gpsData.AddTime,GPS当前时间,已经转为8时区,所以要转为UTC时间
 8            DateTime dt = gpsData.AddTime.ToUniversalTime();
 9
10            st.wYear = (ushort)dt.Year;
11            st.wMonth = (ushort)dt.Month;
12            st.wDay = (ushort)dt.Day;
13            st.wHour = (ushort)(dt.Hour) ;
14            st.wMinute = (ushort)dt.Minute;
15            st.wSecond = (ushort)dt.Second;
16            Win32.SetSystemTime(ref st);     
17
18
19        }
    

 

 

posted @ 2008-03-25 23:48  一支鱼刺  阅读(859)  评论(5编辑  收藏  举报