Android Thread for a timer

public class MainActivity extends Activity { int min, sec; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); min = 5; sec = 0; final TextView timer1 = (TextView) findViewById(R.id.timer1); timer1.setText(min + ":" + sec); Thread t = new Threa

定时器的Android线程

public class MainActivity extends Activity { int min, sec; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); min = 5; sec = 0; final TextView timer1 = (TextView) findViewById(R.id.timer1); timer1.setText(min + ":" + sec); Thread t = new Threa

Custom fonts and XML layouts (Android)

I'm trying to define a GUI layout using XML files in Android. As far as I can find out, there is no way to specify that your widgets should use a custom font (eg one you've placed in assets/font/) in XML files and you can only use the system installed fonts. I know that, in the Java code, I could change the font of each widget manually using unique IDs. Alternatively, I could iterate

自定义字体和XML布局(Android)

我试图在Android中使用XML文件来定义GUI布局。 据我所知,无法指定您的小部件应该使用XML文件中的自定义字体(例如,您放置在assets / font /中的字体),并且只能使用系统安装的字体。 我知道,在Java代码中,我可以使用唯一ID手动更改每个小部件的字体。 或者,我可以遍历Java中的所有小部件来进行此更改,但这可能会很慢。 我还有什么其他选择? 有没有更好的方法来制作具有自定义外观的小部件? 我不是特别想要手动

how to reschedule Timer from within TimerTask run

basically what I want to do is to make a Timer that runs a specific TimerTask after x seconds, but then the TimerTask can reschedule the Timer to do the task after y seconds. Example is below, it gives me an error "Exception in thread "Timer-0" java.lang.IllegalStateException: Task already scheduled or cancelled" on line where I try to schedule this task in TimerTask run. im

如何从TimerTask运行中重新安排Timer

基本上我想要做的是使一个Timer在x秒后运行特定的TimerTask,但是然后TimerTask可以重新安排Timer在y秒后执行任务。 示例如下,它给我一个错误“线程中的异常”Timer-0“java.lang.IllegalStateException:任务已经调度或取消”在我尝试在TimerTask运行中调度此任务的线上。 import java.util.Timer; import java.util.TimerTask; public class JavaReminder { public JavaReminder(int seconds) { Timer timer = new

Rescheduling Timer in android

I'm trying to have a Timer that schedules a TimerTask for successive executions. I don't know how many times it'll work. on a user click, the Timer will stop, and on another, it'll run again. Everything works until stopping, when i reschedule, I get exceptions saying that the Timer is canceled or already scheduled. How do I make it stop and run again? I've tried everythin

在android中重新调度计时器

我想要一个计时器TimerTask连续执行。 我不知道它会工作多少次。 在用户单击时,计时器将停止,并在另一个计时器上再次运行。 一切工作,直到停止,当我重新安排,我收到异常说,定时器被取消或已经安排。 我如何让它停止并再次运行? 我试过了我能想到的一切,但仍然无法正常工作。 这是我正在尝试的最新代码: Timer myTimer; /** Called when the activity is first created. */ @Override public void onCre

java.util.Timer: Is it deprecated?

I read in a comment to this answer and in many other questions about scheduling (sorry, no references) that java.util.Timer is deprecated. I really hope not since I'm using it as the light way to schedule things in Java (and it works nicely). But if it's deprecated, I'll look elsewhere. However, a quick look at the API docs for 1.6 doesn't say anything about it being deprecated

java.util.Timer:是否被弃用?

我在对这个答案的评论中以及有关调度(抱歉,没有引用)的许多其他问题中阅读了java.util.Timer已弃用。 我真的希望没有,因为我使用它作为在Java中安排事情的轻松方式(它很好地工作)。 但如果它被弃用,我会在其他地方看看。 但是,快速浏览1.6版的API文档并没有提到它被弃用的任何内容。 Sun的弃用列表中甚至没有提及它。 它是否被正式弃用?如果是这样,我应该使用什么? *另一方面, 如果它不被弃用,人们是否可以

Pause the timer and then continue it

Refer to the code that @Yuri posted from here. How to stop a timer after certain number of times . If I wanted to stop it because of some condition and then restart it again. How would I go about doing it? private final static int DELAY = 10000; private final Handler handler = new Handler(); private final Timer timer = new Timer(); private final TimerTask task = new TimerTask(

暂停计时器,然后继续

请参阅@Yuri从这里发布的代码。 如何在一定次数后停止定时器。 如果由于某种原因想要停止它,然后重新启动它。 我该怎么去做呢? private final static int DELAY = 10000; private final Handler handler = new Handler(); private final Timer timer = new Timer(); private final TimerTask task = new TimerTask() { private int counter = 0; public void run() { handler.

How to stop TimerTask already in execution?

How to stop a running TimerTask I have gone through the above link and my problem didn't solve completely. We can stop a Timer, by calling timerObj.cancel() . The documentation of cancel method clearly says: Cancels the {@code Timer} and all scheduled tasks. If there is a currently running task it is not affected . No more tasks may be scheduled on this {@code Timer}. Subsequent cal

如何停止已经执行的TimerTask?

如何停止正在运行的TimerTask 我已经通过上述链接,我的问题没有完全解决。 我们可以通过调用timerObj.cancel()来停止Timer。 cancel方法的文件清楚地说: 取消定时器和所有计划的任务。 如果当前正在运行的任务不受影响 。 可以在此{@Code Timer}上安排更多任务。 随后的通话什么都不做 所以我的问题在于, 如果很多TimerTasks都是在一个timer下进行调度的,那么即使它们正在执行它们各自的run方法 ,我们又怎么

Java long running timer is being consumed by GC

I have a java.util.Timer and I am scheduling TimerTasks that run every 24 hours at 3:00:00 AM, however after 5-8 days the Timer is consumed by GC and all my tasks are cancelled because the timer instance no longer exists. How can I prevent this ? I use: t.schedule(timerTask, date); And then the tasks starts again itself (when the task is executed is also calls method that schedules this tas

GC正在使用Java长时间运行计时器

我有一个java.util.Timer,我调度TimerTasks,每24小时在凌晨3:00:00运行,但是5-8天之后Timer被GC消耗,我的所有任务都被取消,因为定时器实例不再运行存在。 我怎样才能防止这一点? 我用: t.schedule(timerTask, date); 然后这些任务自己再次启动(当任务执行时也是再次调度此任务的调用方法) 计时器是这样定义的: public static Timer t = new java.util.Timer(); static变量是定义class一部分,并在加载类时进

How to create a timer task which can be cancelled and rescheduled

I have an application which needs to be run every nth minute of an hour n can be 5 , 15 , 30 etc... So I wrote a timer which will be called every 1 minute and I will do current time in minutes % n to check if its time to schedule. Now problem is that once the task is scheduled at nth minute , I want the timer to pause as it will take more than one minute to complete. I want to know , is there

如何创建可以取消和重新计划的计时器任务

我有一个应用程序,需要运行一个小时,每n分钟n可以是5,15,30等等...所以我写了一个计时器,每1分钟就会调用一次,我将以分钟为单位执行当前时间%n检查是否有时间安排。 现在的问题是,一旦任务安排在第n分钟,我想让计时器暂停,因为它需要一分多钟才能完成。 我想知道,有没有办法在作业安排时取消定时器,并在作业完成后重新安排时间。 我尝试使用timer.cancel,但它会抛出IllegalState异常。

Stopping scheduled task and resheduling it

In my program I'm in need of using timeouts in GUI. For instance, the user starts using GUI, the timeout starts couting. When he reaches some stage, the current timeout is being discarded and new is started. I was thinking about using Timer & TimerTask classes to do it in such manner : TimerTask1 -> do some stuff (including Timer cancel & purge) TimerTask2 -> do another

停止计划任务并重新计划它

在我的程序中,我需要在GUI中使用超时。 例如,用户开始使用GUI,超时开始couting。 当他到达某个阶段时,当前的超时将被丢弃并且新的启动。 我正在考虑使用Timer&TimerTask类以这种方式来完成它: TimerTask1 - >做一些东西(包括定时器取消和清除) TimerTask2 - >做另一个东西(包括定时器取消和清除) 定时器 - >两个任务的一个定时器,因为它们不会并行运行。 代码如下所示: === user starts usin