利用CTelephony获取电量,充电和手机信号的信息 转 - [symbian]

来源:互联网 发布:电脑mac地址怎么改 编辑:程序博客网 时间:2024/05/21 13:54

利用CTelephony获取电量,充电和手机信号的信息 转 - [symbian]

2009-07-06

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://iovi.blogbus.com/logs/41972666.html

1. 声明观察器类 (StateVarObserver.h):

// 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;
};

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);
        }
    }