利用CTelephony获取电量,充电和手机信号的信息 转 - [symbian]
2009-07-06
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://iovi.blogbus.com/logs/41972666.html
1. 声明观察器类 (StateVarObserver.h):
|
2. 实现 (StateVarObserver.cpp):
// StateVarObserver.cpp
// Constructor
CStateVarObserver::CStateVarObserver(TUid aUid) : CActive(EPriorityStandard),
iSigStrengthV1Pckg(iSigStrengthV1),
iBatteryInfoV1Pckg(iBatteryInfoV1),
iIndicatorV1Pckg(iIndicatorV1),
iObservingUid(aUid)
{
//nothing to do
}
// 2nd phase constructor
void CStateVarObserver::ConstructL()
{
iTelephony = CTelephony::NewL();
// Add this active object to the active scheduler
CActiveScheduler::Add(this);
}
void CStateVarObserver::StartL()
{
if ( iObservingUid == KUidNetworkStrength )
{
// 获取手机信号强度
iTelephony.GetSignalStrength(iStatus, iSigStrengthV1Pckg);
}
else if ( iObservingUid == KUidBatteryBars )
{
// 获取手机电量信息
iTelephony.GetBatteryInfo(iStatus,iBatteryInfoV1Pckg);
}
else if ( iObservingUid == KUidChargerStatus )
{
// 获取手机的指示器信息,详见SDK
iTelephony.GetIndicator(iStatus, iIndicatorV1Pckg);
}
else
{
User::Leave(KErrNotSupported);
}
SetActive();
}
// Handles request completion - called by the Active Scheduler
void CStateVarObserver::RunL()
{
if( iStatus.Int() != KErrNone )
{
//TODO error handling
return;
}
//Read once, now register to observe changes
if ( iObservingUid == KUidNetworkStrength )
{
iObservedValue = iSigStrengthV1.iBar;
iTelephony.NotifyChange(iStatus,CTelephony::ESignalStrengthChange,
iSigStrengthV1Pckg);
}
else if ( iObservingUid == KUidBatteryBars )
{
// iBatteryInfoV1.iChargeLevel gives k*14 ( k = {0,1,2,3,4,5,6,7} )
iObservedValue = iBatteryInfoV1.iChargeLevel;
iTelephony.NotifyChange(iStatus,CTelephony::EBatteryInfoChange,
iBatteryInfoV1Pckg);
}
else if ( iObservingUid == KUidChargerStatus )
{
//this same technique can be used to see if there's
//network available (with KIndNetworkAvailable)
//and if there's a call in progress (KIndCallInProgress)
if(iIndicatorV1.iIndicator & CTelephony::KIndChargerConnected)
{
iObservedValue = 1;
}
else
{
iObservedValue = 0;
}
iTelephony.NotifyChange(iStatus,CTelephony::EIndicatorChange,
iIndicatorV1Pckg);
}
else
{
//TODO error handling
}
SetActive();
}
// Cancel outstanding request - called by Cancel()
void CStateVarObserver::DoCancel()
{
if ( iObservingUid == KUidNetworkStrength )
{
iTelephony.CancelAsync(CTelephony::EGetSignalStrengthCancel);
iTelephony.CancelAsync(CTelephony::ESignalStrengthChangeCancel);
}
else if ( iObservingUid == KUidBatteryBars )
{
iTelephony.CancelAsync(CTelephony::EGetBatteryInfoCancel);
iTelephony.CancelAsync(CTelephony::EBatteryInfoChangeCancel);
}
else if ( iObservingUid == KUidChargerStatus )
{
iTelephony.CancelAsync(CTelephony::EGetIndicatorCancel);
iTelephony.CancelAsync(CTelephony::EIndicatorChangeCancel);
}
}