如何取消TimerTask的安全? [英] How to cancel TimerTask safely?

查看:255
本文介绍了如何取消TimerTask的安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发Android的应用程序,我用定时的TimerTask 对象特定时间后执行一个任务。但是有一个问题 - 有时我需要取消TimerTask的,但如果我这样做,是Android将导致 IllegalStateException异常。我怎样才能安全地取消任务?

I have been developing the Android application and I use Timer and TimerTask objects for executing one task after particular time. But there is one problem - sometimes I need to cancel timertask, but if I do it that Android will cause an IllegalStateException. How can I cancel the task safely?

推荐答案

您不能重复使用的TimerTask 秒。

你之所以获得 IllegalStateException异常是(最有可能),因为您尝试安排有previously被取消的任务。

The reason why you get IllegalStateException is (most likely) because you try to schedule a task that has previously been canceled.

要去做正确的,即避免了 IllegalStateException异常,你需要创建一个新的的TimerTask 每次要安排它。

To do it properly, i.e. to avoid the IllegalStateException, you need to create a fresh TimerTask each time you want to schedule it.

这篇关于如何取消TimerTask的安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆