Symbian學(xué)習(xí)筆記(7)——定時器
但是CTimer本身就已經(jīng)是源于CActive了,所以我今天來討論的是直接使用定時器,必竟在手機上定時器是一個比較常用的功能(在BREW開發(fā)中因為沒有多線程,幾乎所有的應(yīng)用都會用上那個ISHELL_SetTimer)。
CTimer有兩個子類CPeriodic和CHeartbeat,都可以處理周期性的定時器回調(diào),其中心跳當(dāng)然是更有規(guī)律一些了,它的使用也稍稍麻煩一點。
先看看心跳的使用吧。修改一下我們的一個視圖:
......{
//省略部分代碼
public:
void Beat();
void Synchronize();
void StartTimer();
private:
CEikLabel* iLabel;
TInt total;
public:
CHeartbeat* iHeart;
}
其中MBeating接口定義了兩個方法Beat(每次心跳時調(diào)一下它)和Synchronize(跟系統(tǒng)時鐘同步一下心跳頻率)。
...{
CreateWindowL();
創(chuàng)建一個標(biāo)準(zhǔn)優(yōu)先級的心率定時器
iHeart=CHeartbeat::NewL(CActive::EPriorityStandard);
iLabel=new(ELeave)CEikLabel;
iLabel->SetContainerWindowL(*this);
SetRect( aRect );
ActivateL();
}
在每次心跳的時候?qū)otal加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的方法***個參數(shù)ETwelveOClock在枚舉TTimerLockSpec中定義,按1/12到1秒這樣劃分定時間隔。
如果我們想用CPeriodic來做定時器的話,不需要實現(xiàn)什么接口了,只需要在Start的時候提供一個回調(diào)函數(shù)就可以了。
【編輯推薦】