×
Namespaces

Variants
Actions
Revision as of 06:57, 16 December 2011 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Monitorando status da bateria usando CTelephony

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

Artigo
Tradução:
Por ivocalado
Última alteração feita por hamishwillee em 16 Dec 2011

A classe CBatteryCheck mostra como checar e monitorar o status da bateria em dispositivos S60 3rd Edition usando a API CTelephony.

A implementação é muito simples, e quando usado a única coisa a se fazer na classe invocadora é implementar uma interface para callback e então construir uma instância de CBatteryCheck.

Quando uma instância de CBatteryCheck é construída, o nível inicial da bateria é checado com a função GetBatteryInfo()- e após o retorno desta função reinvocar RunL, todas as alterações posteriores no status da bateria serão moritorado chamando NotifyChange para alteração no status.

Na função de callback, a variável aChangeStatus armazena o valor corrente do percentual da bateria restante, e a variável aStatus indica o status da bateria com a ajuda do enum de CTelephony:

enum TBatteryStatus
{
/**
The phone software can not determine the phone's current power status.
*/

EPowerStatusUnknown,
 
/**
The phone is powered by the battery.
*/

EPoweredByBattery,
 
/**
A battery is connected, but the phone is externally powered.
*/

EBatteryConnectedButExternallyPowered,
 
/**
No battery is connected.
*/

ENoBatteryConnected,
 
/**
Power fault.
*/

EPowerFault
};

Realize a seguinte linkagem:

LIBRARY etel3rdparty.lib

BatteryLevel.cpp

#include "BatteryLevel.h"
 
CBatteryCheck* CBatteryCheck::NewLC(MBatteryObserver& aObserver)
{
CBatteryCheck* self = new (ELeave) CBatteryCheck(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
CBatteryCheck* CBatteryCheck::NewL(MBatteryObserver& aObserver)
{
CBatteryCheck* self = CBatteryCheck::NewLC(aObserver);
CleanupStack::Pop(); // self;
return self;
}
 
void CBatteryCheck::ConstructL(void)
{
iTelephony = CTelephony::NewL();
GetBatteryInfo();
}
 
CBatteryCheck::~CBatteryCheck()
{
Cancel();
delete iTelephony;
}
 
CBatteryCheck::CBatteryCheck(MBatteryObserver& aObserver)
: CActive(EPriorityStandard),iObserver(aObserver),iBatteryInfoV1Pckg(iBatteryInfoV1)
{
CActiveScheduler::Add(this);
}
 
void CBatteryCheck::GetBatteryInfo()
{
if(iTelephony && !IsActive())
{
iGettingBattery = ETrue;
iTelephony->GetBatteryInfo(iStatus, iBatteryInfoV1Pckg);
SetActive();
}
}
 
void CBatteryCheck::RunL()
{
iObserver.BatteryLevelL(iBatteryInfoV1.iChargeLevel,iBatteryInfoV1.iStatus);
 
if(iStatus != KErrCancel)
{
iTelephony->NotifyChange(iStatus,CTelephony::EBatteryInfoChange,
iBatteryInfoV1Pckg);
SetActive();
}
 
iGettingBattery = EFalse;
}
 
void CBatteryCheck::DoCancel()
{
if(iGettingBattery)
iTelephony->CancelAsync(CTelephony::EGetBatteryInfoCancel);
else
iTelephony->CancelAsync(CTelephony::EBatteryInfoChangeCancel);
}

BatteryLevel.h

#include <Etel3rdParty.h>
 
class MBatteryObserver
{
public:
virtual void BatteryLevelL(TUint aChargeStatus, CTelephony::TBatteryStatus aStatus) = 0;
};
 
class CBatteryCheck : public CActive
{
public:
~CBatteryCheck();
static CBatteryCheck* NewL(MBatteryObserver& aObserver);
static CBatteryCheck* NewLC(MBatteryObserver& aObserver);
 
private:
CBatteryCheck(MBatteryObserver& aObserver);
void ConstructL(void);
 
private:
void GetBatteryInfo();
void RunL();
void DoCancel();
 
private:
MBatteryObserver& iObserver;
CTelephony* iTelephony;
CTelephony::TBatteryInfoV1 iBatteryInfoV1;
CTelephony::TBatteryInfoV1Pckg iBatteryInfoV1Pckg;
TBool iGettingBattery;
};
This page was last modified on 16 December 2011, at 06:57.
90 page views in the last 30 days.