Getting battery information of a windows CE 5.0 device

Deadly 提交于 2019-12-01 12:17:58

You need to P/Invoke GetSystemPowerStatusEx. An example is actually found in this old MSDN article.

I have used the P/Invoke GetSystemPowerStatusEx before and found it caused memory leaks when calling it frequently. For reliable event driven battery status I'd go down the route of SystemState Events.

using Microsoft.WindowsMobile.Status;
_batteryState = new SystemState(SystemProperty.PowerBatteryState) { ComparisonType = StatusComparisonType.AnyChange };
_batteryState.Changed += (o, e) => UpdateBatteryIcon();

private void UpdateBatteryIcon()
{
    var batteryLevel = SystemState.PowerBatteryStrength;
    var isOnCharge = IsOnCharge(SystemState.PowerBatteryState);
    _batteryWarning = batteryLevel == BatteryLevel.VeryLow && !isOnCharge;
    pictBattery.Image = GetBatteryIcon(batteryLevel, isOnCharge);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!