鴻蒙OS IntervalTimer

2022-08-25 09:53 更新

IntervalTimer

java.lang.Object

|---ohos.utils.IntervalTimer

public abstract class IntervalTimer
extends Object

為任務(wù)執(zhí)行提供一個(gè)計(jì)時(shí)器,該計(jì)時(shí)器持續(xù)一段時(shí)間并定期重復(fù)。

示例代碼:

 new IntervalTimer(60000, 3000) {
    @Override
     public void onInterval(long remain) {
         // This method will be called at the 3000th millisecond and repeatedly called every 3000 milliseconds.
     }


    @Override
     public void onFinish() {
         // This method will be called at the 60000th millisecond.
     }
 }.schedule();

注意:IntervalTimer#onInterval(long) 是同步的。 如果此方法的執(zhí)行時(shí)間長(zhǎng)于間隔,則下一次調(diào)用將等待上一次回調(diào)完成。 例如,如果間隔為 3000 毫秒,并且 IntervalTimer#onInterval(long) 回調(diào)持續(xù) 4000 毫秒,則第一次調(diào)用在第 3000 毫秒,下一次調(diào)用在第 9000 毫秒。

構(gòu)造函數(shù)摘要

構(gòu)造函數(shù) 描述
IntervalTimer(long duration, long interval) 創(chuàng)建具有給定工作持續(xù)時(shí)間和間隔的 IntervalTimer。

方法總結(jié)

修飾符和類(lèi)型 方法 描述
void cancel() 取消間隔計(jì)時(shí)器。
abstract void onFinish() 當(dāng)間隔計(jì)時(shí)器的總工作持續(xù)時(shí)間結(jié)束時(shí)調(diào)用。
abstract void onInterval(long remain) 每次間隔開(kāi)始時(shí)調(diào)用。
void schedule() 啟動(dòng)間隔計(jì)時(shí)器。
從類(lèi) java.lang.Object 繼承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

構(gòu)造函數(shù)詳細(xì)信息

IntervalTimer

public IntervalTimer(long duration, long interval)

創(chuàng)建具有給定工作持續(xù)時(shí)間和間隔的 IntervalTimer。

參數(shù):

參數(shù)名稱(chēng) 參數(shù)描述
duration 間隔定時(shí)器的工作時(shí)長(zhǎng),單位毫秒,不能小于interval。 當(dāng)工作時(shí)間結(jié)束時(shí),將調(diào)用 IntervalTimer#onFinish() 通知間隔計(jì)時(shí)器已關(guān)閉。
interval 間隔時(shí)間,單位毫秒,取值范圍為(1,duration]。每開(kāi)始一個(gè)間隔,就會(huì)調(diào)用IntervalTimer#onInterval(long)來(lái)通知間隔定時(shí)器的剩余工作時(shí)間。

方法詳情

schedule

public final void schedule()

啟動(dòng)間隔計(jì)時(shí)器。

Throws:

Throw名稱(chēng) Throw描述
IllegalArgumentException 如果間隔小于或等于 0,或大于工作持續(xù)時(shí)間,則引發(fā)此異常。

cancel

public final void cancel()

取消間隔計(jì)時(shí)器。

onInterval

public abstract void onInterval(long remain)

每次間隔開(kāi)始時(shí)調(diào)用。

參數(shù):

參數(shù)名稱(chēng) 參數(shù)描述
remain 指示間隔定時(shí)器的剩余工作時(shí)間。 由于精度,該值可能重復(fù)。

onFinish

public abstract void onFinish()

當(dāng)間隔計(jì)時(shí)器的總工作持續(xù)時(shí)間結(jié)束時(shí)調(diào)用。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)