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





















