Know Active Timer - android

In Android, after starting any timer using TIMER or CountDownTimer, is there any way to track them?
For example, if we start 5 timer and we want to stop the second one(or update it), how to do that stuff?
Thanks in advance.

put all of them in a list, set timer's tag an id (id from the list) and set listener. so when any of the timers are ticking or finish, you will be notified and you can identify them by id from the tag

Related

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();

How to make a countdown timer run in background in android?

I am revoking a timer in android using intent now if i want to put that timer in a background when user clicks on directly back button then how do i do that? and then when i see background apps running it should be working and can be brought to front.
The problem is that if you define the counter in your Activity, the counter is bound to the Activity life-cycle. In order to workaround this, you can create your counter in a Service and just visualize the value in Activity.
There are also several techniques which you can use when defining the counter inside the Activity. Using SystemClock to track the time in your Activity in onStart() and onStop() for example, you can manually calculate the difference between those two values and adjust accordingly your counter.

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.

Stop Timer when Activity loses focus

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.

Categories

Resources