您可以在应用程序中侦听Power State更改。该文档显示它仅适用于Windows Mobile 6.5,但我在运行.NET CF 3.5的Windows Mobile 5.0应用程序中使用它。
就像手机一样,如果移动设备长时间不使用,操作系统会显示电池在实际使用之前处于相同的电平。所以,这不是很可靠。
但是,您可以侦听事件(like POWER_STATE_CRITICAL等),并让您的软件进行相应地与您的WCF服务器交互的更改。
下面是我使用的东西的编辑版本。
这不会解决你的问题100%,但它应该让你知道如何去做你需要的东西。
using Microsoft.WindowsMobile.Status;
BatteryLevel _batteryLevel;
BatteryState _batteryState;
void Mobile5_Load(object sender, EventArgs e) {
_batteryLevel = (BatteryLevel)SystemState.GetValue(SystemProperty.PowerBatteryStrength);
_batteryState = (BatteryState)SystemState.GetValue(SystemProperty.PowerBatteryState);
if (!BatteryCritical(false)) {
// Continue
}
}
/// <summary>
/// Sets the Battery Level and Battery State for the Mobile Device
/// <para><value>showDialog=True show the dialog box</value></para>
/// <para><value>Returns True <b>if</b> the battery is in a critical state</value></para>
/// </summary>
/// <param name="showDialog">Do you want a dialog box to be displayed or not?</param>
/// <returns>false if the Battery is NOT in the critical state</returns>
bool BatteryCritical(bool showDialog) {
_batteryAlert = false;
bool bad = false; // everything starts out ok. We are actually running, after all.
_batteryLevel = (BatteryLevel)SystemState.GetValue(SystemProperty.PowerBatteryStrength);
_batteryState = (BatteryState)SystemState.GetValue(SystemProperty.PowerBatteryState);
bool present = ((_batteryState & BatteryState.NotPresent) != BatteryState.NotPresent);
bool charging = ((_batteryState & BatteryState.Charging) == BatteryState.Charging);
bool critical = ((_batteryState & BatteryState.Critical) == BatteryState.Critical);
bool lowbatry = ((_batteryState & BatteryState.Low) == BatteryState.Low);
Color c;
if (present) {
if (charging) {
c = Color.Cyan;
} else {
if (critical) {
c = Color.Orange;
_batteryAlert = true;
} else if (lowbatry) {
c = Color.Yellow;
_batteryAlert = true;
} else {
c = Color.White;
}
}
} else {
c = Color.Silver;
}
StatusPanel.BackColor = c;
switch (_batteryLevel) {
case BatteryLevel.VeryHigh: // (81-100%)
BatteryPanel.BackColor = Color.Cyan;
break;
case BatteryLevel.High: // (61-80%)
BatteryPanel.BackColor = Color.Lime;
break;
case BatteryLevel.Medium: // 41-60%)
BatteryPanel.BackColor = Color.Yellow;
break;
case BatteryLevel.Low: // (21-40%)
BatteryPanel.BackColor = Color.Orange;
//WirelessUpdate(RadioState.Off, false);
break;
case BatteryLevel.VeryLow: // (0-20%)
BatteryPanel.BackColor = Color.Red;
//WirelessUpdate(RadioState.Off, false);
bad = (!charging && present);
break;
}
if (showDialog) {
string msg = string.Format("Level is {0}\r\nState is {1}", _batteryLevel, _batteryState);
if (_batteryLevel == BatteryLevel.Low) {
msg += "\r\n\r\nThe wireless radio will be inactive until it has been charged.";
} else if (bad == true) {
msg += "\r\n\r\nThis Application will now close to preserve the device. Please return it to a charging base immediately.";
}
MessageBox.Show(msg, "Battery Meter", MessageBoxButtons.OKCancel, MessageBoxIcon.None, 0);
}
if (!bad) {
StatusPanel.Refresh();
// You could signal your app here
} else {
// Tell your app this device needs to turn off now.
}
return bad;
}
请给我一个upvote,如果它有帮助。
标记为答案如果它解决了您的问题。
为什么不添加一个只是SillAlive(UserName)函数的WCF服务,并从设备的窗体应用程序中的后台线程或计时器中调用该服务? – josef 2013-03-04 18:50:02
WCF需要知道在特定时间内哪些设备处于活动状态。由于某些设备最终可能会冻结或耗尽电池或无法使用(无法使用wi-fi范围),因此我无法简单地依赖于被召唤的服务商。这就是为什么我在控制使用SQL的状态。 – Andrew 2013-03-04 19:54:01