My question is for an Androip App. I am planning to use several timers and they will change the textviews' values once per second. But when I use CountDownTimer
, textviews become unsynchronized. How can I create an exact timer that won't delay and I can see every textview change at the same time?
Asked
Active
Viewed 36 times
0

Antti Haapala -- Слава Україні
- 129,958
- 22
- 279
- 321

Orhan Özdemir
- 1
- 1
-
2change every textview in the same timer. For example, create an object with a list for your textview and a timer. With the timer, proceed the list. – Tokazio Mar 31 '16 at 13:53
-
But my major problem is timer doesn't tick every exact 1 second. It ticks late or earlier sometimes and after an hour it has a difference like 9-10 seconds. I mean is there a different method without delays ? – Orhan Özdemir Mar 31 '16 at 14:02
-
Timers depending on how you use your cpu. http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime – Tokazio Mar 31 '16 at 14:05
-
I tried to use nanoTime() but i need to setText() after 1 second. When I tried to execute my app naturally It tried to setText() for every nanosecond and just freezed. I couldn't find the way to make it per second instead of per nanosecond. – Orhan Özdemir Mar 31 '16 at 14:14
-
use 1000000000 factor (1000000000ns = 1s) – Tokazio Mar 31 '16 at 14:16
-
Not like this I mean I used It like while(finish>start) a.setText(currenttime); – Orhan Özdemir Mar 31 '16 at 14:19
-
You've seen/tested my answer at bottom? – Tokazio Mar 31 '16 at 14:21
-
Aded code tags, fixed some grammar (that I'd understand), added code tags. – Antti Haapala -- Слава Україні Mar 31 '16 at 14:26
1 Answers
-1
Create a timer in a thread like this:
for(;;){
long startTime = System.nanoTime();
while(System.nanoTime() - startTime<1000000000){//1000000000ns = 1s
//Wait or sleep(0)
}
System.out.println("Action!");//Call your action here
}

Tokazio
- 516
- 2
- 18