Questions tagged [timertask]

Timertask is a Java API class. Logically it represents a task that can be scheduled for one-time or repeated execution by a Timer

A TimerTask implements Runnable, also has a method to cancel the scheduled task, if required. It is used with java.util.Timer (Swing timer fires action events).

A concurrent package contains several classes that are similar to TimerTask by concept but provide additional funcitonality.

917 questions
113
votes
3 answers

Timertask or Handler

Let's say that I want to perform some action every 10 seconds and it doesn't necessarily need to update the view. The question is: is it better (I mean more efficient and effective) to use timer with timertask like here: final Handler handler = new…
keysersoze
  • 2,562
  • 4
  • 21
  • 22
107
votes
13 answers

How to run certain task every day at a particular time using ScheduledExecutorService?

I am trying to run a certain task everyday at 5 AM in the morning. So I decided to use ScheduledExecutorService for this but so far I have seen examples which shows how to run task every few minutes. And I am not able to find any example which…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
67
votes
4 answers

TimerTask vs Thread.sleep vs Handler postDelayed - most accurate to call function every N milliseconds?

What is the most accurate way to call a function every N milliseconds? Thread with Thread.sleep TimerTask Handler with postDelayed I modified this example using Thread.sleep and it's not very accurate. I'm developing a music app that will play…
fxfuture
  • 1,910
  • 3
  • 26
  • 40
59
votes
5 answers

How to execute Async task repeatedly after fixed time intervals

How to make Async task execute repeatedly after some time interval just like Timer...Actually I am developing an application that will download automatically all the latest unread greeting from the server and for that purpose I have to check for…
Waseem
  • 1,392
  • 5
  • 21
  • 30
27
votes
1 answer

Where do I create and use ScheduledThreadPoolExecutor, TimerTask, or Handler?

I need to make my RSS Feed reader check the feed every 10 minutes for new posts, and then parse them if there are new ones. I also need to update the UI about every minute. I have read and heard different things from various sources. My current…
zr00
  • 528
  • 1
  • 7
  • 20
26
votes
5 answers

How to use TimerTask with lambdas?

As you hopefully know you can use lambdas in Java 8, for example to replace anonymous methods. An example can be seen here of Java 7 vs Java 8: Runnable runnable = new Runnable() { @Override public void run() { checkDirectory(); …
skiwi
  • 66,971
  • 31
  • 131
  • 216
24
votes
3 answers

Android: Accessing UI Element from timer thread

public Button stb; static int cnt=0; public ArrayList Butgrp1 = new ArrayList(); Timer myt; TimerTask t; stb.setOnClickListener(new OnClickListener() { public void onClick(View v) { myt.mschedule(new TimerTask() { …
Ajay
  • 1,796
  • 3
  • 18
  • 22
24
votes
3 answers

How do you use a TimerTask to run a thread?

I'm struggling to find documentation for the TimerTask function on Android. I need to run a thread at intervals using a TimerTask but have no idea how to go about this. Any advice or examples would be greatly appreciated.
Alex Haycock
  • 375
  • 2
  • 3
  • 12
22
votes
4 answers

How to pause, and resume a TimerTask/ Timer

I have an animation in my Android app that flashes a TextView different colors. I've used a TimerTask, Timer, and Runnable method to implement this. What I need to do is stop the thread when a user leaves the app during this animation in onPause(),…
embersofadyingfire
  • 523
  • 1
  • 4
  • 16
22
votes
1 answer

Timer already cancelled

I have two timers to manage input(en-queue) and output (dequeue) from a FIFO queue but I keep getting a exception for the dequeueing java.lang.IllegalStateException: Timer already cancelled. I can't place a stop to debug line where the error is…
Vhas
  • 233
  • 1
  • 2
  • 5
18
votes
1 answer

Timer and TimerTask in Android

I need a timer for my program. I have written it and it works fine on PC in emulalator program (Android 1.5/2.2). But it doesn't work on the real device (Android 1.5). What am I doing wrong? TimerTask task = new TimerTask() { public void…
Scit
  • 1,722
  • 4
  • 15
  • 18
18
votes
1 answer

Android Asynctask vs Runnable vs timertask vs Service

What are the differences between these methods (classes)? I want to run a app that runs every 5 seconds, clear the memory when it is finished and when the cpu is in standby mode, that you can run the app. So that the app is not bound to a…
shafqat
  • 183
  • 1
  • 2
  • 5
17
votes
3 answers

Start Android Service after every 5 minutes

I was searching over the internet for last 2 days but I couldn't find any tutorial helpful. I have created a service and I am sending a notification in status bar when the service starts. I want that service to stop after showing the notification…
Kashif Umair Liaqat
  • 1,064
  • 1
  • 18
  • 27
16
votes
4 answers

Android - loop part of the code every 5 seconds

I would like to start repeating two lines of code every 5 seconds when I press the button START and end it, when I press the button STOP. I was trynig with a TimerTask and Handles, but couldn't figure it out how. public class MainActivity extends…
Damijan
  • 183
  • 1
  • 2
  • 7
14
votes
4 answers

How can I change my TimerTask's execution period at runtime?I

How can I change period of Timer at runtime? Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { // read new period period = getPeriod(); …
VextoR
  • 5,087
  • 22
  • 74
  • 109
1
2 3
61 62