相当于Android中的javax.swing.Timer

时间:2011-08-31 16:13:38

标签: java android multithreading timer

Android上是否有类似javax.swing.Timer的内容。我知道如何创建自己的Threads,但有什么东西就像swing-timer一样吗?

4 个答案:

答案 0 :(得分:6)

您可能正在寻找班级android.os.CountDownTimer

您可以继承这样的类:

class MyTimer extends CountDownTimer
{
    public MyTimer(int secsInFuture) {
        super(secsInFuture*1000, 1000); //interval/ticks each second.
    }

    @Override
    public void onFinish() {
        Log.d("mytag","timer finished!");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        //fired on each interval
        Log.d("mytag","tick; " + millisUntilFinished + " ms left");
    }
}

答案 1 :(得分:3)

javax.swing.Timer的要点是它在GUI线程上执行任务(EDT - 事件调度线程)。这样可以安全地操作Swing组件。

在Android中还有EDT,你应该manipulate GUI elements on EDT。要延迟代码然后在EDT上运行,请使用view.postDelayed(runnable)

答案 2 :(得分:1)

还有Java的TimerTask。这是我的代码中的一个例子,我正在播放音频样本:

import java.util.Timer;
import java.util.TimerTask;

// from constructor, shown here out of place
timer = new Timer();

// and in method, again, shown out of place:
        INTERVAL_MILLISECONDS = (int)((double)(bufSize) / (double)(nativeSampleRate * 2) * 1000);
        timer.scheduleAtFixedRate( new TimerTask() {
                public void run() {
                        synchronized(this){
                                track.write(data, 0, bufSize);
                                track.play();
                        }
                }
        }, 0, INTERVAL_MILLISECONDS);

答案 3 :(得分:0)

以下代码正确处理 (1) 在 GUI 线程上调用任务,(2) 当任务花费的时间超过延迟时间时不会淹没 GUI 线程,(3) 计时器停止时的垃圾收集:

import android.os.Handler;
import android.os.Looper;

import java.util.ArrayList;

class ActionEvent {
    public ActionEvent(Object src) {

    }
}

interface ActionListener {
    public void actionPerformed(ActionEvent e);
}

class Timer {
    public Timer(int delay, ActionListener al) {
        addActionListener(al);
        _delay = delay;
    }

    public void addActionListener(ActionListener al) {
        _listeners.add(al);
    }

    private void fireActionPerformed() {
        for (ActionListener al : _listeners.toArray(new ActionListener[0])) {
            al.actionPerformed(new ActionEvent(this));
        }
    }

    private void fireAfterDelay() {
        if (!_bStarted)
            return;
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            public void run() {
                if (_bStarted) {
                    fireActionPerformed();
                    if (_bStarted)
                        fireAfterDelay();
                }
            }
        }, _delay);
    }

    public void start() {
        if (!_bStarted) {
            _bStarted = true;
            fireAfterDelay();
        }
    }

    public void stop() {
        _bStarted = false;
    }

    private final ArrayList<ActionListener> _listeners = new ArrayList<>();
    private int _delay;
    private boolean _bStarted;
}

我加入了类 ActionEvent 和 ActionListener 以提高与 Java Swing 的兼容性。为简单起见,您可以删除 ActionEvent 并将 ActionListener 替换为 Runnable。