1

我正在尝试使用计时器创建一些基于时间的事件。下面的代码在计时器启动 5 秒后成功显示敬酒,但是我需要在 5/10/20 秒时显示敬酒。

我试过使用 if (elapsedTime == 5000 ||elapsedTime == 10000 ),但是当我使用 == 时没有显示吐司。

有任何想法吗?

Chronometer.OnChronometerTickListener mChronoListener = new OnChronometerTickListener() { // listens to journey timer to initiate time based events

    public void onChronometerTick(Chronometer arg0) {

        long elapsedTime = SystemClock.elapsedRealtime() - arg0.getBase();

        if (elapsedTime > 5000)
        {
            Toast.makeText(SafeDrive3Activity.this, "HOUR PASSED",      Toast.LENGTH_LONG).show();

        }

    }
};  
4

1 回答 1

0

尝试这样的事情(如果你想在此后每 10 秒保持一次):

Chronometer.OnChronometerTickListener mChronoListener = new OnChronometerTickListener() { 
    // listens to journey timer to initiate time based events
    int currentNeededTime = 5000;
    public void onChronometerTick(Chronometer arg0) {

        long elapsedTime = SystemClock.elapsedRealtime() - arg0.getBase();

        if (elapsedTime > currentNeededTime)
        {
            Toast.makeText(SafeDrive3Activity.this, "HOUR PASSED",      Toast.LENGTH_LONG).show();
            currentNeededTime += currentNeededTime == 5000 ? 5000 : 10000;
        }
    }
};
于 2012-02-09T15:41:25.117 回答