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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值