获取手机信号和电量

--------------声明观察器类 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);

        }

    }

转载于:https://www.cnblogs.com/xiaojinma/archive/2012/12/19/2825479.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值