重新安排计时器的android

编程入门 行业动态 更新时间:2024-10-18 06:14:13
本文介绍了重新安排计时器的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我怎样才能重新安排一个计时器。我曾试图取消计时器/ TimerTask的和,并安排它采用的方法了。但它显示出异常错误:

异常errorjava.lang.IllegalStateException:TimerTask的已定

code我已经用它:

私人定时器定时=新的定时器(alertTimer,真正的); 公共无效reScheduleTimer(INT持续时间){     timer.cancel();     timer.schedule(TimerTask的,1000L,持续时间* 1000L); }

解决方案

如果你看到Timer.cancel(文档),你会看到这样的:

取消定时器和所有预定的任务。如果有一个当前正在运行的任务,它不会受到影响。没有更多的任务可能安排在这个计时器。后续调用什么也不做。

您需要初始化,当你重新安排一个新的计时器:

编辑:

公共无效reScheduleTimer(INT持续时间){   定时器=新的定时器(alertTimer,真正的);   TimerTask的=新MyTimerTask();   timer.schedule(TimerTask的,1000L,持续时间* 1000L); } 私有类MyTimerTask扩展的TimerTask {   @覆盖   公共无效的run(){     //做的东西   } }

How can I reschedule a timer. I have tried to cancel the timer/timertask and and schedule it again using a method. But its showing an exception error:

Exception errorjava.lang.IllegalStateException: TimerTask is scheduled already

Code I have used it :

private Timer timer = new Timer("alertTimer",true); public void reScheduleTimer(int duration) { timer.cancel(); timer.schedule(timerTask, 1000L, duration * 1000L); }

解决方案

If you see the documentation on Timer.cancel() you'll see this:

"Cancels the Timer and all scheduled tasks. If there is a currently running task it is not affected. No more tasks may be scheduled on this Timer. Subsequent calls do nothing."

You'll need to initialize a new Timer when you are rescheduling:

EDIT:

public void reScheduleTimer(int duration) { timer = new Timer("alertTimer",true); timerTask = new MyTimerTask(); timer.schedule(timerTask, 1000L, duration * 1000L); } private class MyTimerTask extends TimerTask { @Override public void run() { // Do stuff } }

更多推荐

重新安排计时器的android

本文发布于:2023-07-14 10:02:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1104041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!