我们需要能够在手持设备上运行的应用程序中确定设备已经返回到底座。
虽然一种方法可能是观察电池电量是否增加,但这种方法确实是一种解决方法,如果电池电量为100%则会失败。
我们未能成功搜索EMD帮助文件。
答案 0 :(得分:0)
我采取的方法是P / Invoke并检查设备的PowerStatus,所以我创建了一个名为 PowerStatus 的类:
public enum ACLineStatus { Offline = 0, Online = 1, Unknown = 255 }
[Flags]
public enum BatteryStatus { High = 1, Low = 2, Critical = 4, Charging = 8, NoSystemBattery = 128, Unknown =-1 }
public class PowerStatus
{
private struct _SYSTEM_POWER_STATUS_EX
{
public byte ACLineStatus;
public byte BatteryFlag;
public byte BatteryLifePercent;
public byte Reserved1;
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
public byte Reserved2;
public byte BackupBatteryFlag;
public byte BackupBatteryLifePercent;
public byte Reserved3;
public uint BackupBatteryLifeTime;
public uint BackupBatteryFullLifeTime;
}
[DllImport("coredll")]
private static extern uint GetSystemPowerStatusEx(ref _SYSTEM_POWER_STATUS_EX lpSystemPowerStatus,
bool fUpdate);
public ACLineStatus ACLineStatus { get; set; }
public BatteryStatus BatteryStatus { get; set; }
public byte? BatteryLifePercent { get; set; }
public TimeSpan? BatteryLifeTime { get; set; }
public TimeSpan? BatteryFullLifeTime { get; set; }
public BatteryStatus BackupBatteryStatus { get; set; }
public byte? BackupBatteryLifePercent { get; set; }
public TimeSpan? BackupBatteryLifeTime { get; set; }
public TimeSpan? BackupBatteryFullLifeTime { get; set; }
private PowerStatus()
{
}
public static PowerStatus GetCurrent()
{
_SYSTEM_POWER_STATUS_EX powerStatus = new _SYSTEM_POWER_STATUS_EX();
GetSystemPowerStatusEx(ref powerStatus, true);
return new PowerStatus
{
ACLineStatus = (ACLineStatus)powerStatus.ACLineStatus,
BatteryStatus = (BatteryStatus)powerStatus.BatteryFlag,
BatteryLifePercent = powerStatus.BatteryLifePercent.Between((byte)0, (byte)100) ? (byte?)powerStatus.BatteryLifePercent : null,
BatteryLifeTime = powerStatus.BatteryLifeTime >= 0 ? new TimeSpan(0, 0, (int)powerStatus.BatteryLifeTime) as TimeSpan? : null,
BatteryFullLifeTime = powerStatus.BatteryFullLifeTime >= 0 ? new TimeSpan(0, 0, (int)powerStatus.BatteryFullLifeTime) as TimeSpan? : null,
BackupBatteryStatus = (BatteryStatus)powerStatus.BackupBatteryFlag,
BackupBatteryLifePercent = powerStatus.BackupBatteryLifePercent.Between((byte)0, (byte)100) ? (byte?)powerStatus.BackupBatteryLifePercent : null,
BackupBatteryLifeTime = powerStatus.BackupBatteryLifeTime >= 0 ? new TimeSpan(0, 0, (int)powerStatus.BackupBatteryLifeTime) as TimeSpan? : null,
BackupBatteryFullLifeTime = powerStatus.BackupBatteryFullLifeTime >= 0 ? new TimeSpan(0, 0, (int)powerStatus.BackupBatteryFullLifeTime) as TimeSpan? : null
};
}
}
然后在我的"设备" class i有一个定时器,可以在设备停靠时触发事件
public static event EventHandler DeviceDocked;
private static ACLineStatus _lastPowerState = PowerStatus.GetCurrent().ACLineStatus;
private static void timer_Tick(object state)
{
ACLineStatus powerState = PowerStatus.GetCurrent().ACLineStatus;
_timer.Change(Timeout.Infinite, Timeout.Infinite);
if (powerState == ACLineStatus.Online && _lastPowerState == ACLineStatus.Offline)
{
var e = DeviceDocked;
if (e != null)
{
e(null, EventArgs.Empty);
}
}
_lastPowerState = powerState;
_timer.Change(1000, Timeout.Infinite);
}
PowerStatus.GetCurrent()可能就是你所需要的......我希望这会有所帮助:)