您可以侦听应用程序中的电源状态更改。文档显示它仅适用于 Windows Mobile 6.5,但我在运行 .NET CF 3.5 的 Windows Mobile 5.0 应用程序中使用它。
就像手机一样,如果移动设备长时间不使用,操作系统会显示电池处于同一水平,直到它真正被使用。所以,不是很靠谱。
但是,您可以侦听事件(如 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;
}