Stop Timer when Activity loses focus - android

I have a Timer which executes a TimerTask every 30 seconds. I only want the timer to launch a new TimerTask if the Activity is displayed i.e. if the user receives a phone call or launches a new activity the Timer will stop. I then need the Timer to restart when the Activity is re-launched and comes into focus.
Now this should be easy, I override the "onWindowFocusChanged(boolean hasFocus)" method and either start or stop the timer depending on the value of hasFocus. The way I start the timer is to create a new Timer object and TimerTask each time and the way I stop the Timer is to call the cancel() method on the Timer object and set timer to null.
My problem is this doesn't always work, if I launch the activity which has the Timer and switch orientations quickly (to start/stop the Activity) I find the Timer is not always cancelled and I end up with multiple Timers launching TimerTasks at an ever increasing rate.
Am I missing something obvious here? Any help would be greatly appreciated.
Thanks

When I see this, it's because I'm creating 2 CountDownTimers in my app. Because each timer can only read messages of type "1" [see http://www.devdaily.com/java/jwarehouse/android/core/java/android/os/CountDownTimer.java.shtml ], it appeared that I only had 1 timer while my app was running normally. I put a "timer.cancel()" in my onDestroy that took care of one of the timers, allowing the other timer to start receiving all the timing messages--long after the onDestroy had returned.
Canceling any existing timer before creating another took care of the lingering timer.

I had a similar problem, I ended up using post delayed in a Thread and avoided a Timer. Not really a fix though.

Related

how do i call a method from of a service from activity which i started the service and reopened

I have an activity and a service.
I want to start a countdown timer in the service and in the activity.
When I close the activity the timer in the service keeps on going in the background.
When I reopen the activity, I want to get the time remaining from the service's timer and start a new timer in the activity with the same time left. How can I accomplish this?
Bad implementation. You are running service just to run a timer to show the remaining time when you restart the app?
Instead you can do the following.
If you are starting timer now for 5 minutes, then when you start the timer, take current system time in milliseconds and add 5*60*1000, that is, you are recording the time your timer should end and then store this long variable in SharedPreference.
Next time when you start your app, get the value of finish time from SharedPreference and subtract it from your current time again. This will directly give you the remaining time in milliseconds.
No Service, no background thread, no additional running code, just a shared preference in the beginning of the timer will keep the record for you.

How to implement countdown timer [re-open activity]?

How i should implement a countdown timer if i need the following usage:
User starts activity A;
In activity A he starts the timer;
User leaves activity A, timer runs in background;
User comes back to activity A;
User see current (updated) values of the timer;
I tried to use a Countdown Timer but it fails at step 5 - after returning to ativity A i cant see values in TextView (in LogCat i do) and system starts another timer (this way, every activity re-open starts one more timer). Should I use service instead or ... show me the way, please.
You can't be sure that after you leave your activity it will be still alive. System could kill it at any time.
The solutions for this use case could be:
Start timer in foreground Service. Such types of services have better chances to be alive.
When you leave your activity you could save last timer value and last timestamp and restore timer with this info after activity is recreated.
PS. If you also want to fire the alarm when timer is finished you should use AlarmManager but keep in mind restrictions from android 6+ (M).

How to create a timer runed in the background and display one second of a second dynamically

How to create a timer runed in the background and display one second of a second in the TextView dynamically in MainActivity.Whatever I exit app or leaving the MainActivity,the timer also keep on.
I thinked that I can use service and BroadcastReceiver or using Handler and Thread.But I can't solve it.
Having an always-on service to simply count up seconds is a bad idea, it'll waste battery life and could be killed at any time.
What you could do is something like this:
Make a counter that you can start/stop with a button.
On your your activity's onPause, save the System.currentTimeInMillis() along with the current count on your timer
When you resume your activity, use the current System.currentTimeInMillis() value to calculate what your timer should be displaying had it been really running all that time.
I have thinked a solution; Thread+Handler; EveryTime enter ther MainActivity
we just set the time interval = EndTime-System.currentTimeMillis();

TimeDelay error in android using thread

As i am making an app, there is problem in delay of the function,
Firstly I am using thread as a timer and I am also using an animation function....
When the timer ends it starts TextToSpeech and in background the animation of Images works continuosly...
After TTS ends it again starts the timer and after a fixed time timer stops and TTS is started..This works on continuosly..
But due to this there is problem in delay of TTS and it does not work exactly after the timer stops....and it takes some time....
So what should I do to stop this delay??
Thanks in advance
Your timer shouldnt have a fixed value, but a calculated one. If you want it to fire at a fixed internal (timeFI), you should calculate the processing time in the TTS (timeTTS) and set the timer "sleep time" = (timeFI - timeTTS)

Android: start listener after timing

i've an activity with a ToggleButton that must start (if checked on) or stop (if checked off) a listener.
The problem is that i want to start the listener after a specific time (10 sec for example) but keeping the ToggleButton active and if the user click on it before the timing ending, abort the timing and listener activation.
I'm in confusion with the correct way to do that... do you have any idea?
thanks in advance
Use a Timer and TimerTask.
Put the timer on 10 seconds delay.
If the user clicks the button again on these 10 seconds, cancel the Timer.
If not, start the listener.
Note that in order to change UI elements through the TimerTask thread, you will have to use an handler.

Categories

Resources