×
Namespaces

Variants
Actions

监控电池状态属性

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 01 Jul 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS000902


  • 开发伙伴平台:

S60 3rd Edition, FP1


  • 详细描述

下列代码片段可以用来接受电池状态改变的通知信息,这个类可以注册用来监听KPSUIDHWRMPowerState它包括了Publish&Subscribe键(用来监控电池状态的改变,电量的改变以及充电状态的改变)


该代码可以自签名执行。


MMP文件

需要下列链接库

LIBRARY  euser.lib


头文件

//batteryobserver.h 
#ifndef BATTERYSTATEOBSERVER_H
#define BATTERYSTATEOBSERVER_H
 
// INCLUDES
#include <e32base.h>
#include <e32property.h>
 
class CBatteryStateObserver : public CActive
{
enum {EPriority=0};
public:
static CBatteryStateObserver* NewL( const TUid aUid, const TUint32 aKey);
virtual ~CBatteryStateObserver();
 
private:
CBatteryStateObserver( const TUid aUid, const TUint32 aKey);
void ConstructL();
void RunL();
void DoCancel();
private:
RProperty iProperty;
const TUid iUid;
const TUint32 iKey;
};
 
#endif // BATTERYSTATEOBSERVER_H


源文件

#include <HWRMPowerStateSDKPSKeys.h>
#include "batteryobserver.h"
 
CBatteryStateObserver::CBatteryStateObserver(
const TUid aUid, const TUint32 aKey)
 : CActive(EPriority), iUid( aUid ),
iKey( aKey )
{
}
 
CBatteryStateObserver* CBatteryStateObserver::NewL(const TUid aUid, const TUint32 aKey)
{
CBatteryStateObserver* self=
new(ELeave) CBatteryStateObserver(aUid, aKey);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
void CBatteryStateObserver::ConstructL()
{
User::LeaveIfError(iProperty.Attach(iUid, iKey));
 
CActiveScheduler::Add(this);
// initial subscription and process current property value
RunL();
}
 
CBatteryStateObserver::~CBatteryStateObserver()
{
Cancel();
iProperty.Close();
}
 
void CBatteryStateObserver::DoCancel()
{
iProperty.Cancel();
}
 
void CBatteryStateObserver::RunL()
{
//resubscribe before processing new value to prevent missing updates
iProperty.Subscribe( iStatus );
SetActive();
 
switch(iKey)
{
case KHWRMBatteryLevel:
{
//KHWRMBatteryLevel updated, do something...
break;
}
case KHWRMBatteryStatus:
{
//KHWRMBatteryStatus updated, do something...
break;
}
case KHWRMChargingStatus:
{
//KHWRMChargingStatus updated, do something...
break;
}
default:
break;
}
 
// property updated, get new value
TInt keyValue;
if( iProperty.Get( keyValue ) == KErrNotFound )
{
// property deleted, do necessary actions here...
}
else
{
// use new value...
/*
- Enumerations for EPSHWRMBatteryLevel(Battery level of device)
EBatteryLevelUnknown Uninitialized or some other error.
EBatteryLevelLevel0 Lowest battery level.
EBatteryLevelLevel1
EBatteryLevelLevel2
EBatteryLevelLevel3
EBatteryLevelLevel4
EBatteryLevelLevel5
EBatteryLevelLevel6
EBatteryLevelLevel7 Highest battery level.
 
- Enumerations for EPSHWRMBatteryStatus(Battery status of device)
EBatteryStatusUnknown Uninitialized or some other error.
EBatteryStatusOk This can also be used during charging.
EBatteryStatusLow Show note to user "Battery low".
EBatteryStatusEmpty Show "recharge battery" note to user.
 
- Enumerations for EPSHWRMChargingStatus(Charging status of device)
EChargingStatusError Some error has occurred when charger is
connected or charging.
EChargingStatusNotConnected Charger not connected/uninitialized.
EChargingStatusCharging Device is charging.
EChargingStatusNotCharging Charger is connected, device not charging.
EChargingStatusAlmostComplete Charging almost completed.
EChargingStatusChargingComplete Charging completed.
EChargingStatusChargingContinued Charging continued after brief interruption.
*/
 
}
}


使用CBatteryStateObserver类

  • 在头文件中:
CBatteryStateObserver* iBatteryStatusObserver;
CBatteryStateObserver* iBatteryLevelObserver;
CBatteryStateObserver* iChargingStatusObserver;
  • 在源文件中
#include <HWRMPowerStateSDKPSKeys.h>
//create the observers
iBatteryStatusObserver =
CBatteryStateObserver::NewL(KPSUidHWRMPowerState,KHWRMBatteryStatus);
iBatteryLevelObserver =
CBatteryStateObserver::NewL(KPSUidHWRMPowerState,KHWRMBatteryLevel);
iChargingStatusObserver =
CBatteryStateObserver::NewL(KPSUidHWRMPowerState,KHWRMChargingStatus);//delete the observers
delete iBatteryStatusObserver;
delete iBatteryLevelObserver;
delete iChargingStatusObserver;
This page was last modified on 15 December 2011, at 08:44.
86 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×