Battery Status on Windows Mobile

大家知道,我们可以通过 Start->Settings->Systems->Power 来查看系统的电池情况,包括电池的类型,剩余的电量等等,如下图 1 所示:

1Windows Mobile自带的Power查看界面

    但是如果再细化到电压、温度、电流等情况的话,我们只有自己来实现了。查阅了MSDN之后发现,托管的SystemState类中,关于Power的成员如下表1所示,并没有细化到电池的电压、温度、电流情况,只有主电池、备用电池的电量高低。

1  SystemState类中Power有关的成员

PowerBatteryBackupState

Gets the current backup battery state (for example, it is low, and charging). This enumeration allows a bitwise combination of its member values.

PowerBatteryBackupStrength

Gets the remaining backup battery power level, expressed as a percentage of fully charged.

PowerBatteryState

Gets the current battery state (for example, it is low, and charging). This enumeration allows a bitwise combination of its member values.

PowerBatteryStrength

Gets the remaining battery power level, expressed as a percentage of fully charged.

    而Native方法中有一个GetSystemPowerStatusEx2,返回SYSTEM_POWER_STATUS_EX2结构体。其包含的字段如下:  

ContractedBlock.gif ExpandedBlockStart.gif Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt  BYTE ACLineStatus;
  BYTE BatteryFlag;
  BYTE BatteryLifePercent;
  BYTE Reserved1;
  DWORD BatteryLifeTime;
  DWORD BatteryFullLifeTime;
  BYTE Reserved2;
  BYTE BackupBatteryFlag;
  BYTE BackupBatteryLifePercent;
  BYTE Reserved3;
  DWORD BackupBatteryLifeTime;
  DWORD BackupBatteryFullLifeTime;
  DWORD BatteryVoltage;
  DWORD BatteryCurrent;
  DWORD BatteryAverageCurrent;
  DWORD BatteryAverageInterval;
  DWORD BatterymAHourConsumed;
  DWORD BatteryTemperature;
  DWORD BackupBatteryVoltage;
  BYTE BatteryChemistry;

    在该结构体中,包括我们所需的电池电压、温度、电流情况。那么,如何来实现呢?Joel Ivory JohnsonWindows Mobile Power Management给出了实现方法。

    首先,我们将所需要的Native Method通过Platform. invoke的方式引入到我们的工程中,

ContractedBlock.gif ExpandedBlockStart.gif Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt    [DllImport("CoreDLL")]

        
public static extern int GetSystemPowerStatusEx2(

             SYSTEM_POWER_STATUS_EX2 statusInfo, 

            
int length,

            
int getLatest

                );

        
public static SYSTEM_POWER_STATUS_EX2 GetSystemPowerStatus()

        {

            SYSTEM_POWER_STATUS_EX2 retVal 
= new SYSTEM_POWER_STATUS_EX2();

           
int result =  GetSystemPowerStatusEx2( retVal, Marshal.SizeOf(retVal) , 1);

            
return retVal;

        }

    然后在我们的应用逻辑中,加入GetSystemPowerStatus()函数,并且将结果显示在界面上。程序运行效果如下图2所示:

图2:程序运行效果

    我们可以改变模拟器的电池状态来看看程序的运行情况,如下图3所示,改变Emulator Properties中的Battery

3:改变Emulator Properties中的Battery

    再看程序的运行界面如下图4所示:

4:改变Battery后的程序界面

    可以看到,电池的剩余电量部分发生了改变,与我们设置的值相等。不过,由于我使用的是模拟器,所以电压、电流等显示有问题。在我的真实设备上,程序运行的界面如下图5所示:

  

5:Cingular8125上的程序界面

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14766526/viewspace-563204/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14766526/viewspace-563204/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值