Countdowntimer works in short intervals, but not others - android

I am facing an interesting situation, maybe someone could explain to me why.
I am doing timer that runs at interval of 100 msec. At every tick, a textview is updated to show the time. Also there is a an edit text with a text watched in which the string is compared 2 times to different strings After each Text changed
When I use Timer class, then the thread seems to be busy because I can't type any text into the edit text . When I use handler same problem happens. But if I use countdowntimer class then it works.
Interestingly, if I increase the interval to 1 sec then all of the above works.
1-is 100 msec too short? I didint think the above computations I making would take that long.
2- why countdowntimer worked and other failed?
Thank you

Related

Android: Is there a way to "setDuration" for a loop to running ? use Thread.Sleep?

I am trying to making a for loop to finish running around 5 seconds. I am trying to figure out a way to do this by maybe calculating a Thread.Sleep method for take up time to fill the set 5 seconds for maybe a 8 loop count. Is there a better way to do this or should I use a Timer Task or related?
I am looking for a efficient and smart way to do this. thanks.
You can use this code also
SystemClock.sleep(5000);
This is 5 second delay

Showing Hours in Chronometer

How do you show hours in chronometer.By default its format is (hh:mm:ss), doesn't show in the view.I need it to show like 00:00:00 , and update the time.
I also require to pause the time and resume the time.
You must have to see at documentation to see how to use format for Chronometer.
This thread could also help you to see an implementation of a chronometer.

Android Chronometer - One second "ticks" late

The app is a sports timer for cycling, skiing etc, where racers start at regular intervals. e.g. 1 minute.
In my implementation of OnChronometerTickListener I notice that the calls occur at intervals significantly longer than 1000 mS. I use the elapsed time (between the Tick and the Chronometer's base) to count down the last 5 seconds for each interval. Due to the late callback, I can get ticks at, say,
55,500
56,600
57,750
58,870
59,980
61,110
I can therefor skip a whole second when I use m_Elapsed % 1000.
I have even seen the text in the Chronometer get behind and have to skip a second.
I have no problem with accuracy when I do calculations based on the Chronometer's base time and current system time.
Do I have to write my own Chronometer using finer callbacks?
Or is there some other fix?

Do after specific time, Android Runnable

Ive got an app with a class that implements Runnable. Where a thread is started and the run() methid overridden. This runs my graphics.
1.st question : how often is the run() called upon? i havent set a time for this so it must be a default value?
2.nd question : i want stuff to be done after a certain amount of time (2min,5min,10min) etc. What would be the best way to go about doing this, i was thinking about using an int as an counter and once it hits a specific value does what i want.
1.st question : how often is the run() called upon? i havent set a time for this so it must be a default value?
The run() method in your Thread is called when you call it eg. yourThread.start();
2.nd question : i want stuff to be done after a certain amount of time (2min,5min,10min) etc. What would be the best way to go about doing this, i was thinking about using an int as an counter and once it hits a specific value does what i want.
There are to options. Either you could call Thread.sleep() method (NB: Never do this in your UI thread).
Or you can do it the way you described above. So in your run() method you would have a while() loop and check on every iteration if the difference of the lastUpdate and the current time in milli seconds is bigger than the wanted period eg. 2 min, 5 min or 10 min.
I hope this helps.
Regarding question 2 - use ScheduledExecutor
1.st question : how often is the run() called upon?
You can find out for yourself, put this at the start of your Runnable:
Log.v("Running Runnable", System.currentTimeMillis() + "");
2.nd question : i want stuff to be done after a certain amount of time (2min,5min,10min) etc.
Extend a HandlerThread (it initializes the Looper for you!), add a Handler as a class variable, and use the Handler's postDelayed() or postAtTime() methods.
The exact amount of time in between calls to run() depends on the processor. The time between each call is the sort of thing that's really visible by the nanosecond. If you're trying to create a timer, I'd recommend using System.currentTimeMillis(), calling it in the run() method, and once the difference is greater than or equal to 1000 milliseconds, the actual timer decrements by one. This will keep track of seconds, and you can use it as a base for minutes and generating other events at specific times.

Android text to speech cuts off another text to speech

so I have a 3 CountDownTimers. 2 of the timers have the same time set. 1 has a different time set. After all timers reach 0, they restart again.
Well after about 3 restarts, the timers catch up with each other.. which is fine... however, I have it set so that at the 20 second mark, Timers 1 & 2 say something in text to speech. And Timer 3 also says something at the 20 second mark. The problem is that the whole phrase is not spoken because they are cutting each other off.
Currently, in all 3 of the countdowns i'm using:
tts.speak("20 seconds remaining.", TextToSpeech.QUEUE_FLUSH, null);
is this problem occuring because i'm using the same TextToSpeech (tts) ?
or maybe it has something to do with QUEUE_FLUSH and null?
And also if I change QUEUE_FLUSH to QUEUE_ADD the text just keeps repeating so that won't work
I resolved this problem by using an onUtteranceCompleted callback that sets a boolean value to true when it's done speaking. This way, the voice doesn't get cut off.

Categories

Resources