Symbian學習筆記(7)——定時器
但是CTimer本身就已經是源于CActive了,所以我今天來討論的是直接使用定時器,必竟在手機上定時器是一個比較常用的功能(在BREW開發中因為沒有多線程,幾乎所有的應用都會用上那個ISHELL_SetTimer)。
CTimer有兩個子類CPeriodic和CHeartbeat,都可以處理周期性的定時器回調,其中心跳當然是更有規律一些了,它的使用也稍稍麻煩一點。
先看看心跳的使用吧。修改一下我們的一個視圖:
......{
//省略部分代碼
public:
void Beat();
void Synchronize();
void StartTimer();
private:
CEikLabel* iLabel;
TInt total;
public:
CHeartbeat* iHeart;
}
其中MBeating接口定義了兩個方法Beat(每次心跳時調一下它)和Synchronize(跟系統時鐘同步一下心跳頻率)。
...{
CreateWindowL();
創建一個標準優先級的心率定時器
iHeart=CHeartbeat::NewL(CActive::EPriorityStandard);
iLabel=new(ELeave)CEikLabel;
iLabel->SetContainerWindowL(*this);
SetRect( aRect );
ActivateL();
}
在每次心跳的時候將total加1,重繪iLabel
...{
this->total++;
if(this->total>100)
...{
this->total=0;
iHeart->Cancel();
}
TBuf<16> buf;
buf.Format(KMsgFormat,this->total);
iLabel->SetTextL(buf);
DrawNow();
}
暫時不用同步
...{
return;
}
//啟動
void CDemoUIAppView::StartTimer()
...{
this->iHeart->Start(ETwelveOClock,this);
}
注意到iHeart->Start的方法***個參數ETwelveOClock在枚舉TTimerLockSpec中定義,按1/12到1秒這樣劃分定時間隔。
如果我們想用CPeriodic來做定時器的話,不需要實現什么接口了,只需要在Start的時候提供一個回調函數就可以了。
【編輯推薦】