countdowntimer.cpp

来自「《基于Symbian OS的手机开发与应用实践》这本书的配套源码。」· C++ 代码 · 共 86 行

CPP
86
字号
/*
* ============================================================================
*  Name     : CCountdownTimer from CountdownTimer.cpp
*  Part of  : Timer
*  Created  : 05.02.2006 by ToBeReplacedByAuthor
*  Implementation notes:
*     
*  Version  :
*  Copyright: ToBeReplacedByCopyright 
* ============================================================================
*/

// INCLUDES
#include "CountdownTimer.h"

// CONSTANTS
const TInt CCountdownTimer::KInterval = 10000;

// public standard construction sequence
CCountdownTimer* CCountdownTimer::NewL(MObserver& aObserver)
    {
    CCountdownTimer* self = CCountdownTimer::NewLC(aObserver);
    CleanupStack::Pop();
    return self;
    }

CCountdownTimer* CCountdownTimer::NewLC(MObserver& aObserver)
    {
    CCountdownTimer* self = new (ELeave) CCountdownTimer(aObserver);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

CCountdownTimer::~CCountdownTimer()
    {
    // No implementation required
    }

// public new functions
void CCountdownTimer::Start(const TTime& aTime)
    {
    Cancel();
    TTime now;
    now.HomeTime();
    iTargetTime = now.Int64() + aTime.Int64();
    After(KInterval);
    }

void CCountdownTimer::Stop()
    {
    Cancel();
    }

// private standard construction sequence
CCountdownTimer::CCountdownTimer(MObserver& aObserver)
:CTimer(EPriorityStandard),iObserver(aObserver)
    {
    // No implementation required
    }

void CCountdownTimer::ConstructL()
    {
    // This is explicitly decareded in the CTimer document
    CTimer::ConstructL();

    // The following line is also essencial!
    CActiveScheduler::Add(this);
    }

// private from CTimer
void CCountdownTimer::RunL()
    {
    TTime now;
    now.HomeTime();
    TTime time = iTargetTime.Int64() - now.Int64();
    if(time.Int64()>0)
        {
        After(KInterval);
        }
    else
        {
        time = 0;
        }
    iObserver.OnTimerL(time);
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?