Creating a timer - android

I'm trying to create a timer, that after the timer ends, will call a function...
For example, I have the function Foo. I want to create a timer, that after 1.5 seconds will call it..
Something like :
Timer(Foo(), 2000);
I have found this code :
private Handler handler = new Handler(); // Creating new handler
handler.postDelayed(runnable, 1500); // Creating a timer for 1.5 seconds
and this function :
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
Foo();
handler.postDelayed(this, 1500);
}
};
My problem is, that some times the timer works perfect, usually for the first 2~3 times, and after that, Instead of being a 1.5sec timer, it become something like 0.3sec timer (and the more handler.postDelayed(runnable, 1500); is being called, the less time the timer will last (like, wont wait 1.5sec to call Foo, but much less)
Why is that ?
I know that in C++ if I write Console Applications, I can use Sleep.. Maybe I can just do something like this :
Sleep(1500);
Foo();
Thanks!
Edit: I have answered my own question.

You could use the Timer class in Android, and set a repeating timer, with a initial delay.
Timer timer = new Timer();
timer.schedule(TimerTask task, long delay, long period)
A TimerTask is very much like a Runnable.
See: http://developer.android.com/reference/java/util/Timer.html

I've used 2 timers :
handler.postDelayed(runnable, 1500); // Creating a timer for 1.5 seconds
this created a 1.5sec timer, while inside the timer loop :
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
Foo();
handler.postDelayed(this, 1500);
}
};
I called handler.postDelayed(this,1500); again, which made 2 timers -> causing the time bug.

Related

Set a callback for MediaPlayer to get called after X seconds

I got a MediaPlayer Object and after, lets say, 5 seconds of the songs time a callback should get called.
How is it possible to archive that with the MediaPlayer?
Or is it necessary to create a wrapper, start a new Thread, wait in that Thread for 5 seconds and call the callback function?
MediaPlayer doesn't have any such thing but one way to accomplish this is with Handler.postDelayed(Runnable, long). That will execute a Runnable after a specific delay.
https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)
Another option is Timer.schedule(TimerTask, long).
https://developer.android.com/reference/java/util/Timer.html#schedule(java.util.TimerTask, long)
Handler is something you are looking for since media player does not have any api for this.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 2000ms
playMusic()
}
}, 2000);

Interrupting execution of handler/timer if user input occurs Android

I am trying to make feature that will display text on screen and after few sec to disappear. I have managed that with Handler and Timer classes.
The problem is that I need to somehow stop executing these timers if user makes input over keyboard before timer's time pass and rerun timer again to display different data.
I am facing with problem that the view has remaining visible after user input and disappears after 1-2 sec instead after 5 sec.
the codes that I have used:
//do something
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
}
AND
//do something
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 5 seconds
//do something again
}
}, 5000);
Can you help me to solve this problem?
THanks
For timer you can use timer.cancel(); and for handler you can use handler.removeCallbacks(runnable);
So just declare a Runnable runnable as a global variable, then instantiate it as below
handler.postDelayed(runnable = new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
this is the runnable you will pass when you removeCallbacks. Similarly you can use this handler.removeCallbacks(null); this will stop all the handlers that have been declared. I would suggest that you declare both the handler and the timer as global variables and only instatntiate them when you are calling the timer tasks.

Run a method every 5 minutes BuzzBox SDK

I need to execute a method every 5 minutes. My problem is where? or How? could be to start the application but did not realize that. Any recommendations?
I don't know how BuzzBox works but I would create a Runnable, then call your method inside it every 5 minutes; That of course uses postDelayed method.
Handler handler = new Handler();
private Runnable updateTimerThread = new Runnable() {
public void run() {
//do call your method here; every 5 minutes
customHandler.postDelayed(this, 5000);
}
};
//you can use your handler anywhere you want like this
handler.postDelayed(updateTimerThread, 5000);

Runnable update lag

My app is a basic clock and i use Runnable for updating the time and the Drawable, but it seems to have some lag in every minute its like 2 or 3 times, its freeze for aboute 2 sec.
This is my runnable code:
private Runnable updateTimeTask = new Runnable() {
public void run() {
updateTime();
handler.postDelayed(this, 1000);
}
Dose someone know what method to use, or how to fix this issue, its not fine for the clock to have missing seconds and drawable lag.
The delay is probably the amount of time it takes to process updateTime(). Try starting the new timer before you call updateTime().
private Runnable updateTimeTask = new Runnable() {
public void run() {
handler.postDelayed(this, 1000);
updateTime();
}

How to Delay Execution of Code For X Amount of Time in Android

On button click I want to begin a timer of 5 minutes and then execute a method that will check for certain conditions and set off alerts if conditions are right. I've seen examples with timers and postDelay, but don't really understand why one would use one vs another. What is the best way to accomplish what I am trying to do? I don't want to lock up the UI during the 5 minutes. The user should be free to use the app as normal during the countdown.
EDIT: I am trying the postDelayed suggestion but visual studio is not liking something about my code. It looks exactly like examples I've found. My be a mono for android thing.
Handler h = new Handler();
Runnable r = new Runnable(){
public void run()
{
Dialog d = inst2.showBuilder(this, "test", "test");
d.Show();
}
};
h.postDelayed(r, 5000);
Specifically the code block inside of run throws all kinds of "} expected" and "a namespace cannot directly contain members such as fields or methods" exceptions.
Try using Timer Object :
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask()
{
#Override
public void run()
{
// Your code goes here
}
}, 1000); // 1sec
final Handler handler = new Handler();
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
#Override
public void run()
{
handler.post(new Runnable()
{
public void run()
{
// YOUR Code
}
});
}
}, 1000); // 1sec
You can start a simple Thread that will sleep in background for 5 minutes and then call a function. While the thread sleeps in background the UI will not freeze. When the thread finish executing what you want you can set off alerts by sending some intents as notifications and receive them in some Broadcast Receivers.
Hope this helps
Use Handler.postDelayed(Runnable block); method to execute delay, as android also not recommend to use timer.
Handler h = new Handler();
Action myAction = () =>
{
// your code that you want to delay here
};
h.PostDelayed(myAction, 1000);

Categories

Resources