获取手机信号和电量
--------------声明观察器类 StateVarObserver.h-------------------------
// Constants
const TInt KUidBatteryBarsValue = 0x100052D3;
const TUid KUidBatteryBars ={KUidBatteryBarsValue};
// KUidChargerStatus defined in <sacls.h>
// KUidNetworkStrength defined in <sacls.h>
// monitoring the info
class CStateVarObserver : public CActive
{
...
private:
CTelephony * iTelephony; //For network, battery and charger information
TUid iObservingUid; //to know what we're observing
TInt iObservedValue; //the value of what we're observing
//Network signal strength information
CTelephony::TSignalStrengthV1 iSigStrengthV1;
CTelephony::TSignalStrengthV1Pckg iSigStrengthV1Pckg;
//Battery information
CTelephony::TBatteryInfoV1 iBatteryInfoV1;
CTelephony::TBatteryInfoV1Pckg iBatteryInfoV1Pckg;
//For charger information
CTelephony::TIndicatorV1 iIndicatorV1;
CTelephony::TIndicatorV1Pckg iIndicatorV1Pckg;
};
--------------------StateVarObserver.cpp-------------------------------
// Constructor
CStateVarObserver::CStateVarObserver(TUid aUid) : CActive(EPriorityStandard),
iSigStrengthV1Pckg(iSigStrengthV1),
iBatteryInfoV1Pckg(iBatteryInfoV1),
iIndicatorV1Pckg(iIndicatorV1),
iObservingUid(aUid)
{
}
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:eave(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:oCancel()
{
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);
}
}