Android: start listener after timing - android

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.

Related

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 check if a view wasn't touched for a specific time

I have a custom view and i want it to do something when the user stops touching it for more then 500 milliseconds.
How can i check this?
I was thinking of having a thread polling the current millisecond time and checking it with the last touch.
is there a better approach with out polling?
Thanks
Every time the user stop touching the view you can open a postdelayed thread for 500ms , in the runnable of the last perform a check if the view has been touched during this period (using a Boolean flag) , if not make the needed operation.
Run a thread that counts 1 for every second, or 1000 for a seconds ( so you have the millis as well) and then just make connection to compare. If u are not touching the UI, u don't need handlers as well I think..

Programmatically 'press' button every 60 seconds

In my app, I've been asked to add an auto update function. What I'm trying to do is have a timer event so that if the user hasn't pressed the update button in the last 60 seconds, do a 'refreshButton.performClick();'.
I've been searching but I can't find an example where the timer interacts with the UI. I keep getting errors like 'only the original thread that created a view hierarchy'.
If the user does press the button, I want to reset the timer.
Is this even possible?
You only can interact with aView from the UI thread.
You can make something like this in your timer task;
Define this variable in your class:
Handler handler = new Handler();
In your timer task call:
handler.post(new Runnable(){
public void run(){
refreshButton.performClick();
}
});
I think you are going in the wrong direction. Instead of trying to press the button, just set a timer to call the same method that is called once the button is pressed, it will make it way simpler, and will not involve the UI for no reason.
You can use AlaramManager class and a background service to run your code on every 60 second.
Here is an example see AlarmService_Service.java

Know Active Timer

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

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