Windows Mobile 5 通知






4.47/5 (8投票s)
如何使用 Windows Mobile 5 网络通知来启动数据复制。


引言
在本文中,我将介绍如何使用 Windows Mobile 5 状态和通知 API (SNAPI) 来获取网络状态更改的通知。集成网络通知是实现后台数据复制的基础。此外,当应用程序了解可用的网络时,它可以选择最佳连接来在设备和后端服务器之间移动数据。
背景
状态和通知 API 为 Windows Mobile 5.0 环境提供了一个强大的内置通知代理结构。SNAPI 的一些要求是
- .NET Compact Framework 版本 2.0
- 通过
Microsoft.WindowsMobile.Status
命名空间公开 - 特定于 Windows Mobile 5.0 平台
- 包含在操作系统安装中(不包含 .NET CF 2.0 的部署 CAB)和模拟器中
Using the Code
要实现应用程序通知,必须添加对以下命名空间的引用
Microsoft.WindowsMobile
Microsoft.WindowsMobile.Status
以下代码概述了如何在应用程序中启用通知回调
public void SetUpNotifications()
{
// This tells which states to monitor
SystemState s;
// Monitor for ActiveSync Connection
s = new SystemState(SystemProperty.CradlePresent);
s.Changed += new ChangeEventHandler(ChangeOccurred);
stateList.Add(s);
// Monitor for GPRS Connection
s = new SystemState(SystemProperty.PhoneGprsCoverage);
s.Changed += new ChangeEventHandler(ChangeOccurred);
stateList.Add(s);
//Monitor for Network Connection (e.g. WiFi)
s = new SystemState(SystemProperty.ConnectionsNetworkCount);
s.Changed += new ChangeEventHandler(ChangeOccurred);
stateList.Add(s);
UpdateConnectionState();
}
public void ChangeOccurred(object sender, ChangeEventArgs args)
{
// If a change occurs
SystemState state = (SystemState)sender;
UpdateConnectionState();
}
public void UpdateConnectionState()
{
// Set the check boxes based on the current state of the networks
activesync.Checked = Convert.ToBoolean
(SystemState.GetValue(SystemProperty.CradlePresent));
gprs.Checked = Convert.ToBoolean(SystemState.GetValue
(SystemProperty.PhoneGprsCoverage));
wifi.Checked = Convert.ToBoolean(SystemState.GetValue
(SystemProperty.ConnectionsNetworkCount));
}
计时器是使用通知 API 来帮助确定何时启动复制的有用方法。在特定间隔,可以调用一个函数来根据网络可用性启动数据移动。以下代码显示了网络当前状态如何帮助确定要检索的数据。
private void Sync()
{
// Initiate A Connection Using the preferred (available) network connection
if (activesync.Checked)
{
// Retrieve all data
}
else if (wifi.Checked)
{
// Retrieve medium priority data
}
else if (gprs.Checked)
{
// Retrieve high priority data
}
else
{
txtStatus.Text = "No Connectivity.";
return;
}
}
结论
SNAPI 是移动应用程序的一个极好的补充,可以帮助优化数据交换。通过添加移动数据库,可以构建一个应用程序,在后台移动数据,以至于用户甚至没有意识到正在执行同步。