Java 如何每15秒运行一次计时器,以及如何取消计时器并重新启动计时器

Java 如何每15秒运行一次计时器,以及如何取消计时器并重新启动计时器,java,android,multithreading,timer,Java,Android,Multithreading,Timer,我的应用程序中有一个条件,那就是如何控制计时器。我想要的是在15秒后,它去启动函数,意思是当它应该被取消时,当函数结束时,它会从零开始重新启动 到目前为止,我所做的是使用计时器,给它1500个延迟和15个重复,但它一次又一次地启动该功能,我认为我在这方面做得不对: timer.schedule(doAsynchronousTask, 1500,1 ); 这是我完整的计时器代码 private final Handler handler = new Handler(); private

我的应用程序中有一个条件,那就是如何控制计时器。我想要的是在15秒后,它去启动函数,意思是当它应该被取消时,当函数结束时,它会从零开始重新启动

到目前为止,我所做的是使用计时器,给它1500个延迟和15个重复,但它一次又一次地启动该功能,我认为我在这方面做得不对:

timer.schedule(doAsynchronousTask, 1500,1 );
这是我完整的计时器代码

    private final Handler handler = new Handler();
private final Timer timer = new Timer();
private final TimerTask task = new TimerTask() {

    public void run() {
        handler.post(new Runnable() {
            public void run() {
                launchFunction();
            }
        });

    }
};
timer.schedule(doAsynchronousTask, 1500,1 );


void launchFunction(){

     Log.d("timer","running");
     timer.cancel();
}
但它没有按预期工作,请帮我解决

阅读文档

/**
     * Schedule a task for repeated fixed-delay execution after a specific delay.
     *
     * @param task
     *            the task to schedule.
     * @param delay
     *            amount of time in milliseconds before first execution.
     * @param period
     *            amount of time in milliseconds between subsequent executions.
     * @throws IllegalArgumentException
     *                if {@code delay < 0} or {@code period <= 0}.
     * @throws IllegalStateException
     *                if the {@code Timer} has been canceled, or if the task has been
     *                scheduled or canceled.
     */
    public void schedule(TimerTask task, long delay, long period) {
        if (delay < 0 || period <= 0) {
            throw new IllegalArgumentException();
        }
        scheduleImpl(task, delay, period, false);
    }
/**
*在特定延迟后,为重复的固定延迟执行计划任务。
*
*@param任务
*要安排的任务。
*@param延迟
*第一次执行前的时间量(毫秒)。
*@参数周期
*后续执行之间的时间量(毫秒)。
*@galargumentException
*如果{@code delay<0}或{@code period阅读文档

/**
     * Schedule a task for repeated fixed-delay execution after a specific delay.
     *
     * @param task
     *            the task to schedule.
     * @param delay
     *            amount of time in milliseconds before first execution.
     * @param period
     *            amount of time in milliseconds between subsequent executions.
     * @throws IllegalArgumentException
     *                if {@code delay < 0} or {@code period <= 0}.
     * @throws IllegalStateException
     *                if the {@code Timer} has been canceled, or if the task has been
     *                scheduled or canceled.
     */
    public void schedule(TimerTask task, long delay, long period) {
        if (delay < 0 || period <= 0) {
            throw new IllegalArgumentException();
        }
        scheduleImpl(task, delay, period, false);
    }
/**
*在特定延迟后,为重复的固定延迟执行计划任务。
*
*@param任务
*要安排的任务。
*@param延迟
*第一次执行前的时间量(毫秒)。
*@参数周期
*后续执行之间的时间量(毫秒)。
*@galargumentException

*如果{@code delay<0}或{@code period try
timer.schedule(doAsynchronousTask,15001500);
第三个参数表示下一次执行的时间段。这意味着在第一次延迟后,方法被调用的时间间隔为毫秒。这对我没有帮助,它是每一个部分启动一次函数。参数为毫秒。即1000毫秒1秒。因此,您必须在15秒内写入15000。第一个参数是异步要启动的TAK。第二个参数是第一次执行任务之前的初始延迟。第三个参数(如上所述)是两次执行之间的时间间隔。描述:延迟->任务任务任务…尝试
timer.schedule(doAsynchronousTask,15001500);
第三个参数表示下一次执行的时间段。这意味着在第一次延迟后,方法被调用的时间间隔为毫秒。这对我没有帮助,它是每一个部分启动一次函数。参数为毫秒。即1000毫秒1秒。因此,您必须在15秒内写入15000。第一个参数是异步要启动的TAK。第二个参数是第一次执行任务之前的初始延迟。第三个参数(如上所述)是两次执行之间的时间间隔。如图所示:延迟->任务。。。